#
# DOIRT_ANES_1980-2016_OIRTexperiments.r
#
#
#
rm(list=ls(all=TRUE))
#
#install.packages(c("doParallel","foreach","foreign","MASS","MCMCpack","DAMisc","rjags","ggplot2","apsrtable","emIRT","gridExtra","dplyr","viridis","margins","ppcor","reshape2","dclone","R2WinBUGS","psych","mcmcplots","forcats"))
#
library(parallel)
library(doParallel)
library(foreach)
library(foreign)
library(MASS)
library(MCMCpack)
library(DAMisc)
library(rjags)
library(ggplot2)
library(apsrtable)
library(emIRT)
library(gridExtra)
library(dplyr)
library(viridis)
library(margins)
library(ppcor)
library(reshape2)
library(dclone)
library(R2WinBUGS)
library(psych)
library(mcmcplots)
library(forcats)
#
#cl <- makeCluster(3)
#registerDoParallel(cl)
#
set.seed(1985)
#
setwd("D:/")
#setwd("c:/Users/cdhare/")
#
#   I. CREATE RESPONSE MATRIX (dat)
#
# 1980
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES1980.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
carter.rating <- V800154
carter.rating[carter.rating < 0] <- NA
carter.rating[carter.rating > 100] <- NA
reagan.rating <- V800155
reagan.rating[reagan.rating < 0] <- NA
reagan.rating[reagan.rating > 100] <- NA
#
carter.reagan.therms <- binary.comparisons(cbind(carter.rating,reagan.rating))
#
#
#  ISSUES
#
libcon <- V800267
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
defensespending <- V800281
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
govtservices <- V800291
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
inflation <- V800301
inflation[inflation < 1] <- NA
inflation[inflation > 7] <- NA
#
abortion.scale <- V800311
abortion.scale[abortion.scale > 4] <- NA
#
taxcut <- V800323
taxcut[taxcut < 1] <- NA
taxcut[taxcut > 5] <- NA
#
aidtoblacks <- V801062
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
russia <- V801078
russia[russia < 1] <- NA
russia[russia > 7] <- NA
#
womensrole <- V801094
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
guarjobs <- V801110
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
ERA <- V801127
ERA[ERA==4] <- 3
ERA[ERA==5] <- 4
ERA[ERA < 1] <- NA
ERA[ERA > 4] <- NA
#
busing <- V801133
busing[busing < 1] <- NA
busing[busing > 7] <- NA
#
schoolprayer <- V801135
schoolprayer[schoolprayer==5] <- 3
schoolprayer[schoolprayer < 1] <- NA
schoolprayer[schoolprayer > 3] <- NA
#
gaydiscrimination <- rep(NA,length(libcon))
gayadoption_2pt <- rep(NA,length(libcon))
gayadoption_4pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
welfarespend <- rep(NA,length(libcon))
poorspend <- rep(NA,length(libcon))
healthinsurance <- rep(NA,length(libcon))
environmentjobs <- rep(NA,length(libcon))
immigrationlevel <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(carter.reagan.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses1980 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(1980,nrow(issues2))
time <- rep(1,nrow(issues2))
#
issues1980 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V800279
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V800278
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement1980 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement1980[is.na(correctplacement1980)] <- 0
correctplacement1980 <- correctplacement1980[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V800721
nonhispwhite[V800721==1 & V800722==5] <- 1
nonhispwhite[V800721!=1] <- 0
nonhispwhite[V800722!=5] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite1980 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V800266
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid1980 <- partyid
#
#  VALUES
#
moraltrad.scores1980 <- rep(NA,nrow(issues2))
egalitarianism.scores1980 <- rep(NA,nrow(issues2))
authoritarianism.scores1980 <- rep(NA,nrow(issues2))
govtrole.scores1980 <- rep(NA,nrow(issues2))
#
detach(data)
#
#
#
#
# 1984
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES1984.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
mondale.rating <- V840301
mondale.rating[mondale.rating < 0] <- NA
mondale.rating[mondale.rating > 100] <- NA
reagan.rating <- V840290
reagan.rating[reagan.rating < 0] <- NA
reagan.rating[reagan.rating > 100] <- NA
#
mondale.reagan.therms <- binary.comparisons(cbind(mondale.rating,reagan.rating))
#
#
#  ISSUES
#
libcon <- V840369
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V840375
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
aidtoblacks <- V840382
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
centralamerica <- V840388
centralamerica[centralamerica < 1] <- NA
centralamerica[centralamerica > 7] <- NA
#
defensespending <- V840395
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
russia <- V840408
russia[russia < 1] <- NA
russia[russia > 7] <- NA
#
guarjobs <- V840414
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
abortion.scale <- V840423
abortion.scale[abortion.scale > 4] <- NA
#
womensrole <- V840250
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
publicschoolspending <- V840998
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
socialsecurityspending <- V840999
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
foodstampspending <- V841000
foodstampspending[foodstampspending < 1] <- NA
foodstampspending[foodstampspending > 3] <- NA
#
medicarespending <- V841001
medicarespending[medicarespending < 1] <- NA
medicarespending[medicarespending > 3] <- NA
#
unemploymentspending <- V841002
unemploymentspending[unemploymentspending < 1] <- NA
unemploymentspending[unemploymentspending > 3] <- NA
#
schoolprayer <- V841038
schoolprayer[schoolprayer==5] <- 2
schoolprayer[schoolprayer > 2] <- NA
schoolprayer[schoolprayer < 1] <- NA
#
healthinsurance <- V841058
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
busing <- V841074
busing[busing < 1] <- NA
busing[busing > 7] <- NA
#
#
gaydiscrimination <- rep(NA,length(libcon))
gayadoption_2pt <- rep(NA,length(libcon))
gayadoption_4pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
welfarespend <- rep(NA,length(libcon))
poorspend <- rep(NA,length(libcon))
environmentjobs <- rep(NA,length(libcon))
immigrationlevel <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(mondale.reagan.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses1984 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(1984,nrow(issues2))
time <- rep(2,nrow(issues2))
#
issues1984 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V840373
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V840374
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement1984 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement1984[is.na(correctplacement1984)] <- 0
correctplacement1984 <- correctplacement1984[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V840708
nonhispwhite[V840708==1 & V840709==5] <- 1
nonhispwhite[V840708!=1] <- 0
nonhispwhite[V840709!=5] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite1984 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V840318
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid1984 <- partyid
#
#  VALUES
#
moraltrad.scores1984 <- rep(NA,nrow(issues2))
egalitarianism.scores1984 <- rep(NA,nrow(issues2))
authoritarianism.scores1984 <- rep(NA,nrow(issues2))
govtrole.scores1984 <- rep(NA,nrow(issues2))
#
detach(data)
#
#
#
#
# 1988
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES1988.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
dukakis.rating <- V880155
dukakis.rating[dukakis.rating < 0] <- NA
dukakis.rating[dukakis.rating > 100] <- NA
bush.rating <- V880154
bush.rating[bush.rating < 0] <- NA
bush.rating[bush.rating > 100] <- NA
#
dukakis.bush.therms <- binary.comparisons(cbind(dukakis.rating,bush.rating))
#
#
#  ISSUES
#
libcon <- V880228
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V880302
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- V880310
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
healthinsurance <- V880318
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
guarjobs <- V880323
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
aidtoblacks <- V880332
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
aidtominorities <- V880340
aidtominorities[aidtominorities < 1] <- NA
aidtominorities[aidtominorities > 7] <- NA
#
socialsecurityspending <- V880348
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
foodstampspending <- V880349
foodstampspending[foodstampspending==7] <- 3
foodstampspending[foodstampspending < 1] <- NA
foodstampspending[foodstampspending > 3] <- NA
#
unemploymentspending <- V880379
unemploymentspending[unemploymentspending==7] <- 3
unemploymentspending[unemploymentspending < 1] <- NA
unemploymentspending[unemploymentspending > 3] <- NA
#
publicschoolspending <- V880383
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
starwars <- V880351
starwars[starwars==7] <- 4
starwars[starwars > 4] <- NA
#
russia <- V880368
russia[russia < 1] <- NA
russia[russia > 7] <- NA
#
importlimits <- V880376
importlimits[importlimits==5] <- 2
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
womensrole <- V880387
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
abortion.scale <- V880395
abortion.scale[abortion.scale > 4] <- NA
#
gaydiscrimination <- V880853
gaydiscrimination[gaydiscrimination==3] <- NA
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
deathpenalty <- V880855
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 5] <- NA
#
affirmativeaction <- V880857
affirmativeaction[affirmativeaction==4] <- 3
affirmativeaction[affirmativeaction==5] <- 4
affirmativeaction[affirmativeaction < 1] <- NA
affirmativeaction[affirmativeaction > 4] <- NA
#
fairjobsblacks <- V880865
fairjobsblacks[fairjobsblacks==5] <- 2
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 2] <- NA
#
schoolprayer <- V880866
schoolprayer[schoolprayer < 1] <- NA
schoolprayer[schoolprayer > 4] <- NA
#
taxcuts <- V880944
taxcuts[taxcuts < 1] <- NA
taxcuts[taxcuts > 5] <- NA
#
#
gayadoption_2pt <- rep(NA,length(libcon))
gayadoption_4pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
welfarespend <- rep(NA,length(libcon))
poorspend <- rep(NA,length(libcon))
environmentjobs <- rep(NA,length(libcon))
immigrationlevel <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
issues <- cbind(dukakis.bush.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses1988 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(1988,nrow(issues2))
time <- rep(3,nrow(issues2))
#
issues1988 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V880235
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V880234
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement1988 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement1988[is.na(correctplacement1988)] <- 0
correctplacement1988 <- correctplacement1988[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V880412
nonhispwhite[V880412==1 &  V880540==5] <- 1
nonhispwhite[V880412!=1] <- 0
nonhispwhite[V880540!=5] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite1988 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V880274
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid1988 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V880951
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V880954
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V880952
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V880953
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores1988 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V880924
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V880925
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V880929
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V880926
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V880927
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V880928
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores1988 <- egalitarianism.scores
#
authoritarianism.scores1988 <- rep(NA,nrow(issues2))
govtrole.scores1988 <- rep(NA,nrow(issues2))
#
detach(data)
#
#
#
#
# 1992
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES1992.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
clinton.rating <- V923306
clinton.rating[clinton.rating < 0] <- NA
clinton.rating[clinton.rating > 100] <- NA
bush.rating <- V923305
bush.rating[bush.rating < 0] <- NA
bush.rating[bush.rating > 100] <- NA
#
clinton.bush.therms <- binary.comparisons(cbind(clinton.rating,bush.rating))
#
#
#  ISSUES
#
libcon <- V923509
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V923701
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- V923707
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
healthinsurance <- V923716
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
parentalleave <- V923717
parentalleave[parentalleave < 1] <- NA
parentalleave[parentalleave > 2] <- NA
#
guarjobs <- V923718
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
foodstampspending <- V923725
foodstampspending[foodstampspending==7] <- 3
foodstampspending[foodstampspending < 1] <- NA
foodstampspending[foodstampspending > 3] <- NA
#
welfarespend <- V923726
welfarespend[welfarespend==7] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
socialsecurityspending <- V923811
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
unemploymentspending <- V923816
unemploymentspending[unemploymentspending==7] <- 3
unemploymentspending[unemploymentspending < 1] <- NA
unemploymentspending[unemploymentspending > 3] <- NA
#
poorspend <- V923817
poorspend[poorspend==7] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
publicschoolspending <- V923818
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
taxesservices <- V925922
taxesservices[taxesservices < 1] <- NA
taxesservices[taxesservices > 2] <- NA
#
aidtoblacks <- V923724
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
abortion.scale <- V923732
abortion.scale[abortion.scale > 4] <- NA
#
parentalconsentabortion <- V923736
parentalconsentabortion[parentalconsentabortion==4] <- 3
parentalconsentabortion[parentalconsentabortion==5] <- 4
parentalconsentabortion[parentalconsentabortion < 1] <- NA
parentalconsentabortion[parentalconsentabortion > 4] <- NA
#
govtfundingabortion <- V923738
govtfundingabortion[govtfundingabortion==4] <- 3
govtfundingabortion[govtfundingabortion==5] <- 4
govtfundingabortion[govtfundingabortion < 1] <- NA
govtfundingabortion[govtfundingabortion > 4] <- NA
#
spousalnotificationabortion <- V923740
spousalnotificationabortion[spousalnotificationabortion==4] <- 3
spousalnotificationabortion[spousalnotificationabortion==5] <- 4
spousalnotificationabortion[spousalnotificationabortion < 1] <- NA
spousalnotificationabortion[spousalnotificationabortion > 4] <- NA
#
womensrole <- V923801
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
importlimits <- V923802
importlimits[importlimits==5] <- 2
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
schoolprayer <- V925945
schoolprayer[schoolprayer < 1] <- NA
schoolprayer[schoolprayer > 4] <- NA
#
deathpenalty <- V925934
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 5] <- NA
deathpenalty[V925933==3] <- 3
#
gaydiscrimination <- V925924
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gaymilitary <- V925926
gaymilitary[gaymilitary==4] <- 3
gaymilitary[gaymilitary==5] <- 4
gaymilitary[gaymilitary < 1] <- NA
gaymilitary[gaymilitary > 4] <- NA
#
gayadoption_4pt <- V925928
gayadoption_4pt[gayadoption_4pt==4] <- 3
gayadoption_4pt[gayadoption_4pt==5] <- 4
gayadoption_4pt[gayadoption_4pt < 1] <- NA
gayadoption_4pt[gayadoption_4pt > 4] <- NA
#
gulfwar <- V923608
gulfwar[gulfwar==5] <- 2
gulfwar[gulfwar < 1] <- NA
gulfwar[gulfwar > 2] <- NA
#
affirmativeaction <- V925936
affirmativeaction[affirmativeaction==4] <- 3
affirmativeaction[affirmativeaction==5] <- 4
affirmativeaction[affirmativeaction < 1] <- NA
affirmativeaction[affirmativeaction > 4] <- NA
#
govtjobsblacks <- V925938
govtjobsblacks[govtjobsblacks==5] <- 2
govtjobsblacks[govtjobsblacks < 1] <- NA
govtjobsblacks[govtjobsblacks > 2] <- NA
#
englishlanguage <- V926233
englishlanguage[englishlanguage==3] <- 2
englishlanguage[englishlanguage==5] <- 3
englishlanguage[englishlanguage < 1] <- NA
englishlanguage[englishlanguage > 3] <- NA
#
immigrationlevel <- V926235
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
#
gayadoption_2pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
guncontrol <- rep(NA,length(libcon))
environmentjobs <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(clinton.bush.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses1992 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(1992,nrow(issues2))
time <- rep(4,nrow(issues2))
#
issues1992 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V923518
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V923517
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement1992 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement1992[is.na(correctplacement1992)] <- 0
correctplacement1992 <- correctplacement1992[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V924202
nonhispwhite[V924202==1 &  V924122==5] <- 1
nonhispwhite[V924202!=1] <- 0
nonhispwhite[V924122!=5] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite1992 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V923634
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid1992 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V926115
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V926118
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V926116
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V926117
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores1992 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V926024
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V926025
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V926029
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V926026
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V926027
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V926028
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores1992 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- V926019
authoritarianism.independence[authoritarianism.independence==5] <- 2
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- V926021
authoritarianism.curiosity[authoritarianism.curiosity==5] <- 2
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- V926020
authoritarianism.obedience[authoritarianism.obedience==5] <- 2
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- V926022
authoritarianism.considerate[authoritarianism.considerate==5] <- 2
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores1992 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V925729
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V925730
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V925731
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores1992 <- govtrole.scores
#
detach(data)
#
#
#
#
# 1996
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES1996.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
clinton.rating <- V960272
clinton.rating[clinton.rating < 0] <- NA
clinton.rating[clinton.rating > 100] <- NA
dole.rating <- V960273
dole.rating[dole.rating < 0] <- NA
dole.rating[dole.rating > 100] <- NA
#
clinton.dole.therms <- binary.comparisons(cbind(clinton.rating,dole.rating))
#
#
#  ISSUES
#
libcon <- V960365
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V960450
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- V960463
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
healthinsurance <- V960479
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
guarjobs <- V960483
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
aidtoblacks <- V960487
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
foodstampspending <- V960496
foodstampspending[foodstampspending==3] <- 4
foodstampspending[foodstampspending==2] <- 3
foodstampspending[foodstampspending==4] <- 2
foodstampspending[foodstampspending==7] <- 3
foodstampspending[foodstampspending < 1] <- NA
foodstampspending[foodstampspending > 3] <- NA
#
welfarespend <- V960497
welfarespend[welfarespend==3] <- 4
welfarespend[welfarespend==2] <- 3
welfarespend[welfarespend==4] <- 2
welfarespend[welfarespend==7] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
socialsecurityspending <- V960560
socialsecurityspending[socialsecurityspending==3] <- 4
socialsecurityspending[socialsecurityspending==2] <- 3
socialsecurityspending[socialsecurityspending==4] <- 2
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- V960562
publicschoolspending[publicschoolspending==3] <- 4
publicschoolspending[publicschoolspending==2] <- 3
publicschoolspending[publicschoolspending==4] <- 2
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
poorspend <- V960565
poorspend[poorspend==3] <- 4
poorspend[poorspend==2] <- 3
poorspend[poorspend==4] <- 2
poorspend[poorspend==7] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
environmentjobs <- V960523
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
environmentalregulations <- V960537
environmentalregulations[environmentalregulations < 1] <- NA
environmentalregulations[environmentalregulations > 7] <- NA
#
taxcuts <- V961283
taxcuts[taxcuts < 1] <- NA
taxcuts[taxcuts > 6] <- NA
#
richpoordifferences <- V961320
richpoordifferences[richpoordifferences < 1] <- NA
richpoordifferences[richpoordifferences > 7] <- NA
#
welfarechildren <- V961322
welfarechildren[welfarechildren==4] <- 3
welfarechildren[welfarechildren==5] <- 4
welfarechildren[welfarechildren < 1] <- NA
welfarechildren[welfarechildren > 4] <- NA
#
welfaretime <- V961324
welfaretime[welfaretime==4] <- 3
welfaretime[welfaretime==5] <- 4
welfaretime[welfaretime < 1] <- NA
welfaretime[welfaretime > 4] <- NA
#
importlimits <- V961327
importlimits[importlimits==5] <- 2
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
fairjobsblacks <- V961207
fairjobsblacks[fairjobsblacks==4] <- 3
fairjobsblacks[fairjobsblacks==5] <- 4
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 4] <- NA
#
affirmativeaction.scale <- V961209
affirmativeaction.scale[affirmativeaction.scale==4] <- 3
affirmativeaction.scale[affirmativeaction.scale==5] <- 4
affirmativeaction.scale[affirmativeaction.scale < 1] <- NA
affirmativeaction.scale[affirmativeaction.scale > 4] <- NA
#
affirmativeaction.pastdisc <- V961268
affirmativeaction.pastdisc[affirmativeaction.pastdisc==4] <- 3
affirmativeaction.pastdisc[affirmativeaction.pastdisc==5] <- 4
affirmativeaction.pastdisc[affirmativeaction.pastdisc < 1] <- NA
affirmativeaction.pastdisc[affirmativeaction.pastdisc > 4] <- NA
#
abortion.scale <- V960503
abortion.scale[abortion.scale > 4] <- NA
#
reducecrime <- V960519
reducecrime[reducecrime < 1] <- NA
reducecrime[reducecrime > 7] <- NA
#
womensrole <- V960543
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
gaydiscrimination <- V961194
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gaymilitary <- V961196
gaymilitary[gaymilitary==4] <- 3
gaymilitary[gaymilitary==5] <- 4
gaymilitary[gaymilitary < 1] <- NA
gaymilitary[gaymilitary > 4] <- NA
#
deathpenalty <- V961198
deathpenalty[deathpenalty==4] <- 3
deathpenalty[deathpenalty==5] <- 4
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 4] <- NA
#
schoolprayer <- V961214
schoolprayer[schoolprayer < 1] <- NA
schoolprayer[schoolprayer > 4] <- NA
#
guncontrol_2pt <- V961217
guncontrol_2pt[guncontrol_2pt==5] <- 2
guncontrol_2pt[guncontrol_2pt < 1] <- NA
guncontrol_2pt[guncontrol_2pt > 2] <- NA
#
immigrationlevel <- V961325
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
#
gayadoption_2pt <- rep(NA,length(libcon))
gayadoption_4pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(clinton.dole.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses1996 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(1996,nrow(issues2))
time <- rep(5,nrow(issues2))
#
issues1996 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V960379
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V960380
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement1996 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement1996[is.na(correctplacement1996)] <- 0
correctplacement1996 <- correctplacement1996[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V960067
nonhispwhite[V960067==1 &  V960708==5] <- 1
nonhispwhite[V960067!=1] <- 0
nonhispwhite[V960708!=5] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite1996 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V960420
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid1996 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V961248
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V961247
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V961250
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V961249
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores1996 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V961229
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V961230
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V961231
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V961232
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V961233
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V961234
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores1996 <- egalitarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V961144
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V961145
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V961146
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores1996 <- govtrole.scores
#
authoritarianism.scores1996 <- rep(NA,nrow(issues2))
#
detach(data)
#
#
#
#
# 2000
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES2000.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
gore.rating <- V000360
gore.rating[gore.rating < 0] <- NA
gore.rating[gore.rating > 100] <- NA
bush.rating <- V000361
bush.rating[bush.rating < 0] <- NA
bush.rating[bush.rating > 100] <- NA
#
gore.bush.therms <- binary.comparisons(cbind(gore.rating,bush.rating))
#
#
#  ISSUES
#
libcon <- V000440
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
libcon.branch <- V000446
libcon.branch[libcon.branch < 1] <- NA
libcon.branch[libcon.branch > 7] <- NA
libcon.branch[V000440!=9] <- NA
#
govtservices <- V000545
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
govtservices.branch <- V000549
govtservices.branch[govtservices.branch < 1] <- NA
govtservices.branch[govtservices.branch > 5] <- NA
#
defensespending <- V000581
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
defensespending.branch <- V000586
defensespending.branch[defensespending.branch < 1] <- NA
defensespending.branch[defensespending.branch > 5] <- NA
#
healthinsurance <- V000609
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
healthinsurance.branch <- V000613
healthinsurance.branch[healthinsurance.branch < 1] <- NA
healthinsurance.branch[healthinsurance.branch > 5] <- NA
#
guarjobs <- V000615
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
guarjobs.branch <- V000619
guarjobs.branch[guarjobs.branch < 1] <- NA
guarjobs.branch[guarjobs.branch > 5] <- NA
#
aidtoblacks <- V000641
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
aidtoblacks.branch <- V000644
aidtoblacks.branch[aidtoblacks.branch < 1] <- NA
aidtoblacks.branch[aidtoblacks.branch > 7] <- NA
#
fairjobsblacks <- V000802
fairjobsblacks[fairjobsblacks==4] <- 3
fairjobsblacks[fairjobsblacks==5] <- 4
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 4] <- NA
#
affirmativeaction.scale <- V000806
affirmativeaction.scale[affirmativeaction.scale==4] <- 3
affirmativeaction.scale[affirmativeaction.scale==5] <- 4
affirmativeaction.scale[affirmativeaction.scale < 1] <- NA
affirmativeaction.scale[affirmativeaction.scale > 4] <- NA
#
affirmativeaction.pastdiscrim <- V000674a
affirmativeaction.pastdiscrim[affirmativeaction.pastdiscrim==4] <- 3
affirmativeaction.pastdiscrim[affirmativeaction.pastdiscrim==5] <- 4
affirmativeaction.pastdiscrim[affirmativeaction.pastdiscrim < 1] <- NA
affirmativeaction.pastdiscrim[affirmativeaction.pastdiscrim > 4] <- NA
#
welfarespend <- V000676
welfarespend[welfarespend==5] <- 2
welfarespend[welfarespend==7] <- 3
welfarespend[welfarespend > 3] <- NA
#
foodstampspending <- V000679
foodstampspending[foodstampspending==5] <- 2
foodstampspending[foodstampspending==7] <- 3
foodstampspending[foodstampspending > 3] <- NA
#
poorspend <- V000680
poorspend[poorspend==5] <- 2
poorspend[poorspend==7] <- 3
poorspend[poorspend > 3] <- NA
#
socialsecurityspending <- V000681
socialsecurityspending[socialsecurityspending==5] <- 2
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- V000683
publicschoolspending[publicschoolspending==5] <- 2
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending > 3] <- NA
#
surplustaxcuts <- V000690
surplustaxcuts[surplustaxcuts==4] <- 3
surplustaxcuts[surplustaxcuts==5] <- 4
surplustaxcuts[surplustaxcuts < 1] <- NA
surplustaxcuts[surplustaxcuts > 4] <- NA
#
environmentjobs <- V000708
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
environmentjobs.branch <- V000712
environmentjobs.branch[environmentjobs.branch < 1] <- NA
environmentjobs.branch[environmentjobs.branch > 5] <- NA
#
environmentalregulations.scale <- V000771
environmentalregulations.scale[environmentalregulations.scale < 1] <- NA
environmentalregulations.scale[environmentalregulations.scale > 7] <- NA
#
environmentalregulations.branch <- V000775
environmentalregulations.branch[environmentalregulations.branch < 1] <- NA
environmentalregulations.branch[environmentalregulations.branch > 5] <- NA
#
schoolvouchers <- V000742
schoolvouchers[schoolvouchers==5] <- 2
schoolvouchers[schoolvouchers < 1] <- NA
schoolvouchers[schoolvouchers > 2] <- NA
#
importlimits <- V000512
importlimits[importlimits==5] <- 2
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
womensrole.scale <- V000755
womensrole.scale[womensrole.scale < 1] <- NA
womensrole.scale[womensrole.scale > 7] <- NA
#
womensrole.branch <- V000759
womensrole.branch[womensrole.branch < 1] <- NA
womensrole.branch[womensrole.branch > 5] <- NA
#
reducecrime.scale <- V001482
reducecrime.scale[reducecrime.scale < 1] <- NA
reducecrime.scale[reducecrime.scale > 7] <- NA
#
reducecrime.branch <- V001486
reducecrime.branch[reducecrime.branch < 1] <- NA
reducecrime.branch[reducecrime.branch > 7] <- NA
#
abortion.scale <- V000694
abortion.scale[abortion.scale < 1] <- NA
abortion.scale[abortion.scale > 4] <- NA
#
abortion.parentalconsent <- V000702
abortion.parentalconsent[abortion.parentalconsent==4] <- 3
abortion.parentalconsent[abortion.parentalconsent==5] <- 4
abortion.parentalconsent[abortion.parentalconsent < 1] <- NA
abortion.parentalconsent[abortion.parentalconsent > 4] <- NA
#
abortion.partialbirth <- V000705
abortion.partialbirth[abortion.partialbirth==4] <- 3
abortion.partialbirth[abortion.partialbirth==5] <- 4
abortion.partialbirth[abortion.partialbirth < 1] <- NA
abortion.partialbirth[abortion.partialbirth > 4] <- NA
#
deathpenalty <- V000752
deathpenalty[deathpenalty==4] <- 3
deathpenalty[deathpenalty==5] <- 4
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 4] <- NA
#
guncontrol_5pt <- V000731
guncontrol_5pt[guncontrol_5pt < 1] <- NA
guncontrol_5pt[guncontrol_5pt > 5] <- NA
#
gaymilitary <- V000727
gaymilitary[gaymilitary==4] <- 3
gaymilitary[gaymilitary==5] <- 4
gaymilitary[gaymilitary < 1] <- NA
gaymilitary[gaymilitary > 4] <- NA
#
gayadoption_2pt <- V000748
gayadoption_2pt[gayadoption_2pt==5] <- 2
gayadoption_2pt[gayadoption_2pt < 1] <- NA
gayadoption_2pt[gayadoption_2pt > 2] <- NA
#
gaydiscrimination <- V001481
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
immigrationlevel <- V000510
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
englishlanguage <- V000745
englishlanguage[englishlanguage==3] <- 2
englishlanguage[englishlanguage==5] <- 3
englishlanguage[englishlanguage < 1] <- NA
englishlanguage[englishlanguage > 3] <- NA
#
#
gayadoption_4pt <- rep(NA,length(libcon))
gaymarriage <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(gore.bush.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses2000 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(2000,nrow(issues2))
time <- rep(6,nrow(issues2))
#
issues2000 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V001382
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V001383
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement2000 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement2000[is.na(correctplacement2000)] <- 0
correctplacement2000 <- correctplacement2000[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V001006a
nonhispwhite[V001006a==50] <- 1
nonhispwhite[V001006a!=50] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite2000 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V000523
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid2000 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V001531
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V001530
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V001533
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V001532
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores2000 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V001521
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V001522
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V001523
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V001524
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V001525
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V001526
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores2000 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- V001586
authoritarianism.independence[authoritarianism.independence==5] <- 2
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- V001588
authoritarianism.curiosity[authoritarianism.curiosity==5] <- 2
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- V001587
authoritarianism.obedience[authoritarianism.obedience==5] <- 2
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- V001589
authoritarianism.considerate[authoritarianism.considerate==5] <- 2
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores2000 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V001420
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V001421
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V001422
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores2000 <- govtrole.scores
#
detach(data)
#
#
#
#
# 2004
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES2004.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
kerry.rating <- V043039
kerry.rating[kerry.rating < 0] <- NA
kerry.rating[kerry.rating > 100] <- NA
bush.rating <- V043038
bush.rating[bush.rating < 0] <- NA
bush.rating[bush.rating > 100] <- NA
#
kerry.bush.therms <- binary.comparisons(cbind(kerry.rating,bush.rating))
#
#
#  ISSUES
#
libcon <- V043085
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V043136
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- V043142
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
bushtaxcuts <- V043149
bushtaxcuts[bushtaxcuts==4] <- 3
bushtaxcuts[bushtaxcuts==5] <- 4
bushtaxcuts[bushtaxcuts < 1] <- NA
bushtaxcuts[bushtaxcuts > 4] <- NA
#
healthinsurance <- V043150
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
guarjobs <- V043152
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
aidtoblacks <- V043158
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
aidtohispanics <- V045140
aidtohispanics[aidtohispanics < 1] <- NA
aidtohispanics[aidtohispanics > 7] <- NA
#
socialsecurityspending <- V043165
socialsecurityspending[socialsecurityspending==5] <- 2
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- V043166
publicschoolspending[publicschoolspending==5] <- 2
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
welfarespend <- V043169
welfarespend[welfarespend==5] <- 2
welfarespend[welfarespend==7] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
poorspend <- V043172
poorspend[poorspend==5] <- 2
poorspend[poorspend==7] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
environmentjobs <- V043182
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
importlimits <- V045114
importlimits[importlimits==5] <- 2
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
socialsecuritystocks <- V045143b
socialsecuritystocks[socialsecuritystocks < 1] <- NA
socialsecuritystocks[socialsecuritystocks > 7] <- NA
#
schoolvouchers <- V045144a
schoolvouchers[schoolvouchers==4] <- 3
schoolvouchers[schoolvouchers==5] <- 4
schoolvouchers[schoolvouchers < 1] <- NA
schoolvouchers[schoolvouchers > 4] <- NA
#
fairjobsblacks <- V045109b
fairjobsblacks[fairjobsblacks==4] <- 3
fairjobsblacks[fairjobsblacks==5] <- 4
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 4] <- NA
#
affirmativeaction <- V045207a
affirmativeaction[affirmativeaction==4] <- 3
affirmativeaction[affirmativeaction==5] <- 4
affirmativeaction[affirmativeaction < 1] <- NA
affirmativeaction[affirmativeaction > 4] <- NA
#
womensrole <- V043196
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
abortion.scale <- V045132
abortion.scale[abortion.scale > 4] <- NA
#
abortion.govtfunds <- V043179
abortion.govtfunds[abortion.govtfunds==4] <- 3
abortion.govtfunds[abortion.govtfunds==5] <- 4
abortion.govtfunds[abortion.govtfunds < 1] <- NA
abortion.govtfunds[abortion.govtfunds > 4] <- NA
#
abortion.partialbirth <- V043181
abortion.partialbirth[abortion.partialbirth==4] <- 3
abortion.partialbirth[abortion.partialbirth==5] <- 4
abortion.partialbirth[abortion.partialbirth < 1] <- NA
abortion.partialbirth[abortion.partialbirth > 4] <- NA
#
deathpenalty <- V043187
deathpenalty[deathpenalty==4] <- 3
deathpenalty[deathpenalty==5] <- 4
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 4] <- NA
#
guncontrol_5pt <- V043189
guncontrol_5pt[guncontrol_5pt < 1] <- NA
guncontrol_5pt[guncontrol_5pt > 5] <- NA
#
gaymarriage <- V043210
gaymarriage[gaymarriage==5] <- 2
gaymarriage[gaymarriage < 1] <- NA
gaymarriage[gaymarriage > 3] <- NA
#
gaydiscrimination <- V045156a
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gaymilitary <- V045157a
gaymilitary[gaymilitary==4] <- 3
gaymilitary[gaymilitary==5] <- 4
gaymilitary[gaymilitary < 1] <- NA
gaymilitary[gaymilitary > 4] <- NA
#
gayadoption_2pt <- V045158
gayadoption_2pt[gayadoption_2pt==5] <- 2
gayadoption_2pt[gayadoption_2pt < 1] <- NA
gayadoption_2pt[gayadoption_2pt > 2] <- NA
#
immigrationlevel <- V045115
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
iraqwar <- V043134
iraqwar[iraqwar==5] <- 2
iraqwar[iraqwar < 1] <- NA
iraqwar[iraqwar > 2] <- NA
#
terrorismspending <- V043174
terrorismspending[terrorismspending==5] <- 2
terrorismspending[terrorismspending==7] <- 3
terrorismspending[terrorismspending < 1] <- NA
terrorismspending[terrorismspending > 3] <- NA
#
diplomacymilitaryforce <- V043107
diplomacymilitaryforce[diplomacymilitaryforce < 1] <- NA
diplomacymilitaryforce[diplomacymilitaryforce > 7] <- NA
#
#
gayadoption_4pt <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_3pt <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(kerry.bush.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses2004 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
index <- 1:nrow(issues2)
year <- rep(2004,nrow(issues2))
time <- rep(7,nrow(issues2))
#
issues2004 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V043090
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V043091
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement2004 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement2004[is.na(correctplacement2004)] <- 0
correctplacement2004 <- correctplacement2004[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V043299
nonhispwhite[V043299==50] <- 1
nonhispwhite[V043299!=50] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite2004 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V043116
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid2004 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V045189
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V045190
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V045191
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V045192
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores2004 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V045212
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V045213
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V045214
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V045215
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V045216
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V045217
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores2004 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- V045208
authoritarianism.independence[authoritarianism.independence==5] <- 2
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- V045209
authoritarianism.curiosity[authoritarianism.curiosity==5] <- 2
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- V045210
authoritarianism.obedience[authoritarianism.obedience==5] <- 2
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- V045211
authoritarianism.considerate[authoritarianism.considerate==5] <- 2
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores2004 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V045152
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V045151
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V045150
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores2004 <- govtrole.scores
#
detach(data)
#
#
#
#
# 2008
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES2008.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#  REMOVE BLACK AND LATINO OVERSAMPLE (310 AFRICAN AMERICANS AND 350 LATINOS
#
black <- V081103a
black[V081103a==22] <- 1
black[V081103a!=22] <- 0
#
latino <- V081103
latino[V081103==1] <- 1
latino[V081103!=1] <- 0
#
set.seed(1985)
black.cut <- as.numeric(sample(rownames(data[black==1,]),310,replace=FALSE))
set.seed(1985)
latino.cut <- as.numeric(sample(rownames(data[latino==1,]),350,replace=FALSE))
#
oversample.cut <- c(black.cut,latino.cut)
#
data.0 <- data[-oversample.cut,]
data.orig2008 <- data
#
detach(data)
#
data <- data.0
attach(data,warn.conflicts = FALSE)
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
obama.rating <- V083037a
obama.rating[obama.rating < 0] <- NA
obama.rating[obama.rating > 100] <- NA
mccain.rating <- V083037b
mccain.rating[mccain.rating < 0] <- NA
mccain.rating[mccain.rating > 100] <- NA
#
obama.mccain.therms <- binary.comparisons(cbind(obama.rating,mccain.rating))
#
#
#  ISSUES
#
libcon <- V083069
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V083105
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
govtservices.new <- V083108x
govtservices.new[govtservices.new < 1] <- NA
govtservices.new[govtservices.new > 7] <- NA
#
defensespending <- V083112
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
defensespending.new <- V083115x
defensespending.new[defensespending.new < 1] <- NA
defensespending.new[defensespending.new > 7] <- NA
#
healthinsurance <- V083119
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
universal.healthcare <- V083124x
universal.healthcare[universal.healthcare < 1] <- NA
universal.healthcare[universal.healthcare > 7] <- NA
#
guarjobs <- V083128
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
immigrant.workperiod <- V083131x
immigrant.workperiod[immigrant.workperiod < 1] <- NA
immigrant.workperiod[immigrant.workperiod > 7] <- NA
#
immigrant.citizenship <- V083133x
immigrant.citizenship[immigrant.citizenship < 1] <- NA
immigrant.citizenship[immigrant.citizenship > 7] <- NA
#
aidtoblacks <- V083137
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
socialsecurityspending <- V083141
socialsecurityspending[socialsecurityspending==5] <- 2
socialsecurityspending[socialsecurityspending==7] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- V083142
publicschoolspending[publicschoolspending==5] <- 2
publicschoolspending[publicschoolspending==7] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
welfarespend <- V083145
welfarespend[welfarespend==5] <- 2
welfarespend[welfarespend==7] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
poorspend <- V083148
poorspend[poorspend==5] <- 2
poorspend[poorspend==7] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
taxrates <- V083152
taxrates[taxrates==2] <- 4
taxrates[taxrates==3] <- 2
taxrates[taxrates==4] <- 3
taxrates[taxrates < 1] <- NA
taxrates[taxrates > 3] <- NA
#
environmentjobs <- V083154
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
emissionstandards.new <- V083157x
emissionstandards.new[emissionstandards.new < 1] <- NA
emissionstandards.new[emissionstandards.new > 7] <- NA
#
socialsecuritystocks <- V085104x
socialsecuritystocks[socialsecuritystocks < 1] <- NA
socialsecuritystocks[socialsecuritystocks > 7] <- NA
#
fairjobsblacks <- V085079a
fairjobsblacks[fairjobsblacks==5] <- 2
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 2] <- NA
#
affirmativeaction <- V085157
affirmativeaction[affirmativeaction==5] <- 2
affirmativeaction[affirmativeaction < 1] <- NA
affirmativeaction[affirmativeaction > 2] <- NA
#
womensrole <- V083166
womensrole[womensrole < 1] <- NA
womensrole[womensrole > 7] <- NA
#
abortion.scale <- V085086
abortion.scale[abortion.scale < 1] <- NA
abortion.scale[abortion.scale > 4] <- NA
#
abortion.birthdefect <- V085096x
abortion.birthdefect[abortion.birthdefect < 1] <- NA
abortion.birthdefect[abortion.birthdefect > 9] <- NA
#
abortion.financialhardship <- V085097x
abortion.financialhardship[abortion.financialhardship < 1] <- NA
abortion.financialhardship[abortion.financialhardship > 9] <- NA
#
death.penalty <- V083163x
death.penalty[death.penalty==4] <- 3
death.penalty[death.penalty==5] <- 4
death.penalty[death.penalty < 1] <- NA
death.penalty[death.penalty > 4] <- NA
#
guncontrol_3pt <- V083164
guncontrol_3pt[guncontrol_3pt == 5] <- 2
guncontrol_3pt[guncontrol_3pt < 1] <- NA
guncontrol_3pt[guncontrol_3pt > 3] <- NA
#
gaydiscrimination <- V083211x
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gay.military <- V083212x
gay.military[gay.military==4] <- 3
gay.military[gay.military==5] <- 4
gay.military[gay.military < 0] <- NA
gay.military[gay.military > 4] <- NA
#
gayadoption_2pt <- V083213
gayadoption_2pt[gayadoption_2pt==5] <- 2
gayadoption_2pt[gayadoption_2pt < 1] <- NA
gayadoption_2pt[gayadoption_2pt > 2] <- NA
#
gaymarriage <- V083214
gaymarriage[gaymarriage == 5] <- 2
gaymarriage[gaymarriage < 1] <- NA
gaymarriage[gaymarriage > 3] <- NA
#
immigrationlevel <- V085082
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
iraqwar <- V083103
iraqwar[iraqwar == 5] <- 2
iraqwar[iraqwar < 1] <- NA
iraqwar[iraqwar > 2] <- NA
#
terrorismspending <- V083150
terrorismspending[terrorismspending==5] <- 2
terrorismspending[terrorismspending==7] <- 3
terrorismspending[terrorismspending < 1] <- NA
terrorismspending[terrorismspending > 3] <- NA
#
#
gayadoption_4pt <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
#
#
issues <- cbind(obama.mccain.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses2008 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
# Put 2008 oversample respondents who were cut back in
oversample.cut2 <- as.numeric(c(oversample.cut,rownames(data[nresponses < 3,])))
A <- match(as.numeric(rownames(data.orig2008)),oversample.cut2)
A[A > 0] <- 0
A[is.na(A)] <- 1
nresponses2008 <- A==1
#
index <- 1:nrow(issues2)
year <- rep(2008,nrow(issues2))
time <- rep(8,nrow(issues2))
#
issues2008 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V083071a
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V083071b
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement2008 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement2008[is.na(correctplacement2008)] <- 0
correctplacement2008 <- correctplacement2008[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V081103a
nonhispwhite[V081103a==21] <- 1
nonhispwhite[V081103a!=21] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite2008 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V083098x
partyid <- partyid + 1
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid2008 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V085139
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V085140
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V085141
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V085142
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores2008 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V085162
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- V085163
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- V085164
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- V085165
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V085166
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V085167
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores2008 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- V085158
authoritarianism.independence[authoritarianism.independence==5] <- 2
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- V085159
authoritarianism.curiosity[authoritarianism.curiosity==5] <- 2
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- V085160
authoritarianism.obedience[authoritarianism.obedience==5] <- 2
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- V085161
authoritarianism.considerate[authoritarianism.considerate==5] <- 2
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores2008 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V085107
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V085106
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V085105
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores2008 <- govtrole.scores
#
detach(data)
#
#
#
#
# 2012
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES2012.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#  REMOVE BLACK AND LATINO OVERSAMPLES
#
latino.cut <- as.numeric(rownames(data[ftf_oversample==1,]))
black.cut <- as.numeric(rownames(data[ftf_oversample==2,]))
latinoblack.cut <- as.numeric(rownames(data[ftf_oversample==3,]))
#
oversample.cut <- c(latino.cut,black.cut,latinoblack.cut)
#
data.0 <- data[-oversample.cut,]
data.orig2012 <- data
#
detach(data)
#
data <- data.0
attach(data,warn.conflicts = FALSE)
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
obama.rating <- ft_dpc
obama.rating[obama.rating < 0] <- NA
obama.rating[obama.rating > 100] <- NA
romney.rating <- ft_rpc
romney.rating[romney.rating < 0] <- NA
romney.rating[romney.rating > 100] <- NA
#
obama.romney.therms <- binary.comparisons(cbind(obama.rating,romney.rating))
#
#
#  ISSUES
#
libcon <- libcpre_self
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- spsrvpr_ssself
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- defsppr_self
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
healthinsurance <- inspre_self
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
guarjobs <- guarpr_self
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
aidtoblacks <- aidblack_self
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
environmentjobs <- envjob_self
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
offshoredrilling <- envir_drill
offshoredrilling[offshoredrilling==2] <- 4
offshoredrilling[offshoredrilling==3] <- 2
offshoredrilling[offshoredrilling==4] <- 3
offshoredrilling[offshoredrilling < 1] <- NA
offshoredrilling[offshoredrilling > 3] <- NA
#
globalwarming <- envir_gwarm
globalwarming[globalwarming < 1] <- NA
globalwarming[globalwarming > 2] <- NA
#
socialsecurityspending <- fedspend_ss
socialsecurityspending[socialsecurityspending==2] <- 4
socialsecurityspending[socialsecurityspending==3] <- 2
socialsecurityspending[socialsecurityspending==4] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- fedspend_schools
publicschoolspending[publicschoolspending==2] <- 4
publicschoolspending[publicschoolspending==3] <- 2
publicschoolspending[publicschoolspending==4] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
welfarespend <- fedspend_welfare
welfarespend[welfarespend==2] <- 4
welfarespend[welfarespend==3] <- 2
welfarespend[welfarespend==4] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
poorspend <- fedspend_poor
poorspend[poorspend==2] <- 4
poorspend[poorspend==3] <- 2
poorspend[poorspend==4] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
socialsecuritystocks <- ssinv_invest_x
socialsecuritystocks[socialsecuritystocks < 1] <- NA
socialsecuritystocks[socialsecuritystocks > 7] <- NA
#
importlimits <- imports_limit
importlimits[importlimits < 1] <- NA
importlimits[importlimits > 2] <- NA
#
millionairetax <- milln_milltax
millionairetax[millionairetax==2] <- 4
millionairetax[millionairetax==3] <- 2
millionairetax[millionairetax==4] <- 3
millionairetax[millionairetax < 1] <- NA
millionairetax[millionairetax > 3] <- NA
#
fairjobsblacks <- fairjob_opin_x
fairjobsblacks[fairjobsblacks==4] <- 3
fairjobsblacks[fairjobsblacks==5] <- 4
fairjobsblacks[fairjobsblacks < 1] <- NA
fairjobsblacks[fairjobsblacks > 4] <- NA
#
affirmativeaction <- aa_work_x
affirmativeaction[affirmativeaction < 1] <- NA
affirmativeaction[affirmativeaction > 7] <- NA
#
abortion.scale <- abortpre_4point
abortion.scale[abortion.scale < 1] <- NA
abortion.scale[abortion.scale > 4] <- NA
#
abortion.birthdefect <- abort_bd_x
abortion.birthdefect[abortion.birthdefect < 1] <- NA
abortion.birthdefect[abortion.birthdefect > 9] <- NA
#
abortion.financial <- abort_fin_x
abortion.financial[abortion.financial < 1] <- NA
abortion.financial[abortion.financial > 9] <- NA
#
deathpenalty <- penalty_favopp_x
deathpenalty[deathpenalty==4] <- 3
deathpenalty[deathpenalty==5] <- 4
deathpenalty[deathpenalty < 1] <- NA
deathpenalty[deathpenalty > 4] <- NA
#
guncontrol_3pt <- gun_control
guncontrol_3pt[guncontrol_3pt==2] <- 4
guncontrol_3pt[guncontrol_3pt==3] <- 2
guncontrol_3pt[guncontrol_3pt==4] <- 3
guncontrol_3pt[guncontrol_3pt < 1] <- NA
guncontrol_3pt[guncontrol_3pt > 3] <- NA
#
gaydiscrimination <- gayrt_discstd_x
gaydiscrimination[gaydiscrimination==4] <- 3
gaydiscrimination[gaydiscrimination==5] <- 4
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gaydiscrimination.new <- gayrt_discrev_x
gaydiscrimination.new[gaydiscrimination.new==4] <- 3
gaydiscrimination.new[gaydiscrimination.new==5] <- 4
gaydiscrimination.new[gaydiscrimination.new < 1] <- NA
gaydiscrimination.new[gaydiscrimination.new > 4] <- NA
#
gaymilitary.old <- gayrt_milstd_x
gaymilitary.old[gaymilitary.old==4] <- 3
gaymilitary.old[gaymilitary.old==5] <- 4
gaymilitary.old[gaymilitary.old < 1] <- NA
gaymilitary.old[gaymilitary.old > 4] <- NA
#
gaymilitary.new <- gayrt_milrev_x
gaymilitary.new[gaymilitary.new==4] <- 3
gaymilitary.new[gaymilitary.new==5] <- 4
gaymilitary.new[gaymilitary.new < 1] <- NA
gaymilitary.new[gaymilitary.new > 4] <- NA
#
gayadoption_2pt <- gayrt_adopt
gayadoption_2pt[gayadoption_2pt < 1] <- NA
gayadoption_2pt[gayadoption_2pt > 2] <- NA
#
gaymarriage <- gayrt_marry
gaymarriage[gaymarriage < 1] <- NA
gaymarriage[gaymarriage > 3] <- NA
#
immigrationpolicy <- immig_policy
immigrationpolicy[immigrationpolicy < 1] <- NA
immigrationpolicy[immigrationpolicy > 4] <- NA
#
dreamact <- immig_citizen
dreamact[dreamact==2] <- 4
dreamact[dreamact==3] <- 2
dreamact[dreamact==4] <- 3
dreamact[dreamact < 1] <- NA
dreamact[dreamact > 3] <- NA
#
checkimmigrationstatus <- immig_checks
checkimmigrationstatus[checkimmigrationstatus==2] <- 4
checkimmigrationstatus[checkimmigrationstatus==3] <- 2
checkimmigrationstatus[checkimmigrationstatus==4] <- 3
checkimmigrationstatus[checkimmigrationstatus < 1] <- NA
checkimmigrationstatus[checkimmigrationstatus > 3] <- NA
#
marijuanalegal <- pot_legal
marijuanalegal[marijuanalegal==2] <- 4
marijuanalegal[marijuanalegal==3] <- 2
marijuanalegal[marijuanalegal==4] <- 3
marijuanalegal[marijuanalegal < 1] <- NA
marijuanalegal[marijuanalegal > 3] <- NA
#
bombiran <- iran_nuksite
bombiran[bombiran==2] <- 4
bombiran[bombiran==3] <- 2
bombiran[bombiran==4] <- 3
bombiran[bombiran < 1] <- NA
bombiran[bombiran > 3] <- NA
#
wiretaps <- wiretap_warrant
wiretaps[wiretaps==2] <- 4
wiretaps[wiretaps==3] <- 2
wiretaps[wiretaps==4] <- 3
wiretaps[wiretaps < 1] <- NA
wiretaps[wiretaps > 3] <- NA
#
immigrationlevel <- immigpo_level
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
#
gayadoption_4pt <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(obama.romney.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses2012 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
# Put 2012 oversample respondents who were cut back in
oversample.cut2 <- as.numeric(c(oversample.cut,rownames(data[nresponses < 3,])))
A <- match(as.numeric(rownames(data.orig2012)),oversample.cut2)
A[A > 0] <- 0
A[is.na(A)] <- 1
nresponses2012 <- A==1
#
index <- 1:nrow(issues2)
year <- rep(2012,nrow(issues2))
time <- rep(9,nrow(issues2))
#
issues2012 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- libcpre_ptyd
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- libcpre_ptyr
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement2012 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement2012[is.na(correctplacement2012)] <- 0
correctplacement2012 <- correctplacement2012[nresponses >=3]
#
#  RACE
#
nonhispwhite <- dem_raceeth_x
nonhispwhite[dem_raceeth_x==1] <- 1
nonhispwhite[dem_raceeth_x!=1] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite2012 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- pid_x
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid2012 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- trad_adjust
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- trad_lifestyle
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- trad_tolerant
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- trad_famval
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores2012 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- egal_equal
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.toofar <- egal_toofar
egalitarianism.toofar[egalitarianism.toofar < 1] <- NA
egalitarianism.toofar[egalitarianism.toofar > 5] <- NA
egalitarianism.toofar <- (-1 * egalitarianism.toofar) + 6
#
egalitarianism.bigprob <- egal_bigprob
egalitarianism.bigprob[egalitarianism.bigprob < 1] <- NA
egalitarianism.bigprob[egalitarianism.bigprob > 5] <- NA
#
egalitarianism.worryless <- egal_worryless
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- egal_notbigprob
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- egal_fewerprobs
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.toofar,egalitarianism.bigprob,
			egalitarianism.worryless,egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores2012 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- auth_ind
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- auth_cur
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- auth_obed
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- auth_consid
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores2012 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- govrole_lessmore
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- govrole_market
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- govrole_big
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores2012 <- govtrole.scores
#
detach(data)
#
#
#
# 2016
#
data <- read.dta("Dropbox/mass_constraint/analysis/ANES2016.dta",convert.factors = FALSE)
attach(data,warn.conflicts = FALSE)
#
#
#  THERMOMETERS
#
binary.comparisons <- function(obj){
    nr <- nrow(obj)
    nc <- ncol(obj)
    combs <- combn(nc, 2)
    res <- sapply(1:ncol(combs), function(i)
        sign(rowSums(cbind(obj[,combs[2,i]], -obj[,combs[1,i]]))))
    res[which(res == 1)] <- 2
    res[which(res == -1)] <- 1
    res[which(res == 0)] <- NA
    res[which(is.na(res))] <- NA
    colnames(res) <- combn(colnames(obj), 2, paste, collapse = "_")
    return(res)
}
#
clinton.rating <- V161086
clinton.rating[clinton.rating < 0] <- NA
clinton.rating[clinton.rating > 100] <- NA
trump.rating <- V161087
trump.rating[trump.rating < 0] <- NA
trump.rating[trump.rating > 100] <- NA
#
clinton.trump.therms <- binary.comparisons(cbind(clinton.rating,trump.rating))
#
#
#  ISSUES
#
libcon <- V161126
libcon[libcon < 1] <- NA
libcon[libcon > 7] <- NA
#
govtservices <- V161178
govtservices[govtservices < 1] <- NA
govtservices[govtservices > 7] <- NA
#
defensespending <- V161181
defensespending[defensespending < 1] <- NA
defensespending[defensespending > 7] <- NA
#
healthinsurance <- V161184
healthinsurance[healthinsurance < 1] <- NA
healthinsurance[healthinsurance > 7] <- NA
#
guarjobs <- V161189
guarjobs[guarjobs < 1] <- NA
guarjobs[guarjobs > 7] <- NA
#
aidtoblacks <- V161198
aidtoblacks[aidtoblacks < 1] <- NA
aidtoblacks[aidtoblacks > 7] <- NA
#
environmentjobs <- V161201
environmentjobs[environmentjobs < 1] <- NA
environmentjobs[environmentjobs > 7] <- NA
#
socialsecurityspending <- V161205
socialsecurityspending[socialsecurityspending==2] <- 4
socialsecurityspending[socialsecurityspending==3] <- 2
socialsecurityspending[socialsecurityspending==4] <- 3
socialsecurityspending[socialsecurityspending < 1] <- NA
socialsecurityspending[socialsecurityspending > 3] <- NA
#
publicschoolspending <- V161206
publicschoolspending[publicschoolspending==2] <- 4
publicschoolspending[publicschoolspending==3] <- 2
publicschoolspending[publicschoolspending==4] <- 3
publicschoolspending[publicschoolspending < 1] <- NA
publicschoolspending[publicschoolspending > 3] <- NA
#
welfarespend <- V161209
welfarespend[welfarespend==2] <- 4
welfarespend[welfarespend==3] <- 2
welfarespend[welfarespend==4] <- 3
welfarespend[welfarespend < 1] <- NA
welfarespend[welfarespend > 3] <- NA
#
poorspend <- V161211
poorspend[poorspend==2] <- 4
poorspend[poorspend==3] <- 2
poorspend[poorspend==4] <- 3
poorspend[poorspend < 1] <- NA
poorspend[poorspend > 3] <- NA
#
abortion.scale <- V161232
abortion.scale[abortion.scale < 1] <- NA
abortion.scale[abortion.scale > 4] <- NA
#
guncontrol_3pt <- V161187
guncontrol_3pt[guncontrol_3pt==2] <- 4
guncontrol_3pt[guncontrol_3pt==3] <- 2
guncontrol_3pt[guncontrol_3pt==4] <- 3
guncontrol_3pt[guncontrol_3pt < 1] <- NA
guncontrol_3pt[guncontrol_3pt > 3] <- NA
#
gaydiscrimination <- V161229x
gaydiscrimination[gaydiscrimination < 1] <- NA
gaydiscrimination[gaydiscrimination > 4] <- NA
#
gayadoption_2pt <- V161230
gayadoption_2pt[gayadoption_2pt < 1] <- NA
gayadoption_2pt[gayadoption_2pt > 2] <- NA
#
gaymarriage <- V161231
gaymarriage[gaymarriage < 1] <- NA
gaymarriage[gaymarriage > 3] <- NA
#
immigrationlevel <- V162157
immigrationlevel[immigrationlevel < 1] <- NA
immigrationlevel[immigrationlevel > 5] <- NA
#
#
gayadoption_4pt <- rep(NA,length(libcon))
guncontrol_2pt <- rep(NA,length(libcon))
guncontrol_5pt <- rep(NA,length(libcon))
libcon.branch <- rep(NA,length(libcon))
govtservices.branch <- rep(NA,length(libcon))
guarjobs.branch <- rep(NA,length(libcon))
healthinsurance.branch <- rep(NA,length(libcon))
environmentjobs.branch <- rep(NA,length(libcon))
aidtoblacks.branch <- rep(NA,length(libcon))
defensespending.branch <- rep(NA,length(libcon))
govtservices.new <- rep(NA,length(libcon))
universal.healthcare <- rep(NA,length(libcon))
defensespending.new <- rep(NA,length(libcon))
#
#
issues <- cbind(clinton.trump.therms,abortion.scale,
		gaydiscrimination,gayadoption_2pt,gayadoption_4pt,gaymarriage,guncontrol_2pt,guncontrol_3pt,guncontrol_5pt,
		welfarespend,poorspend,
		libcon,govtservices,guarjobs,healthinsurance,environmentjobs,aidtoblacks,defensespending,immigrationlevel,
		libcon.branch,govtservices.branch,guarjobs.branch,healthinsurance.branch,environmentjobs.branch,
		aidtoblacks.branch,defensespending.branch,govtservices.new,universal.healthcare,defensespending.new)
#
#
nresponses <- apply(issues, 1, function(x){sum(!is.na(x))})
#
# Select only respondents who supply at least three responses
nresponses2016 <- nresponses >=3
issues2 <- issues[nresponses >=3,]
#
#
index <- 1:nrow(issues2)
year <- rep(2016,nrow(issues2))
time <- rep(10,nrow(issues2))
#
issues2016 <- cbind(index,year,time,issues2)
#
#  POLITICAL KNOWLEDGE FILTER: 1 = CORRECT, 0 = INCORRECT
#
libcon.demparty <- V161130
libcon.demparty[libcon.demparty < 1] <- NA
libcon.demparty[libcon.demparty > 7] <- NA
libcon.repparty <- V161131
libcon.repparty[libcon.repparty < 1] <- NA
libcon.repparty[libcon.repparty > 7] <- NA
#
correctplacement2016 <- as.numeric(libcon.demparty < libcon.repparty)
correctplacement2016[is.na(correctplacement2016)] <- 0
correctplacement2016 <- correctplacement2016[nresponses >=3]
#
#  RACE
#
nonhispwhite <- V161310x
nonhispwhite[V161310x==1] <- 1
nonhispwhite[V161310x!=1] <- 0
nonhispwhite <- nonhispwhite[nresponses >=3]
nonhispwhite2016 <- nonhispwhite
#
#  PARTY ID: 1 = STRONG DEMOCRAT, 7 = STRONG REPUBLICAN
#
partyid <- V161158x
partyid[partyid < 1] <- NA
partyid[partyid > 7] <- NA
partyid <- partyid[nresponses >=3]
partyid2016 <- partyid
#
#  VALUES --- MORAL TRADITIONALISM
#
moraltrad.adjust <- V162207
moraltrad.adjust[moraltrad.adjust < 1] <- NA
moraltrad.adjust[moraltrad.adjust > 5] <- NA
#
moraltrad.lifestyle <- V162208
moraltrad.lifestyle[moraltrad.lifestyle < 1] <- NA
moraltrad.lifestyle[moraltrad.lifestyle > 5] <- NA
moraltrad.lifestyle <- (-1 * moraltrad.lifestyle) + 6
#
moraltrad.tolerant <- V162209
moraltrad.tolerant[moraltrad.tolerant < 1] <- NA
moraltrad.tolerant[moraltrad.tolerant > 5] <- NA
#
moraltrad.familyvalues <- V162210
moraltrad.familyvalues[moraltrad.familyvalues < 1] <- NA
moraltrad.familyvalues[moraltrad.familyvalues > 5] <- NA
moraltrad.familyvalues <- (-1 * moraltrad.familyvalues) + 6
#
moral.matrix <- cbind(moraltrad.adjust,moraltrad.lifestyle,moraltrad.tolerant,moraltrad.familyvalues)
moraltrad.scores <- rowMeans(moral.matrix, na.rm=TRUE)
moraltrad.scores[is.na(moraltrad.scores)] <- NA
#
moraltrad.scores <- moraltrad.scores[nresponses >=3]
moraltrad.scores2016 <- moraltrad.scores
#
#  VALUES --- EGALITARIANISM
#
egalitarianism.equal <- V162243
egalitarianism.equal[egalitarianism.equal < 1] <- NA
egalitarianism.equal[egalitarianism.equal > 5] <- NA
#
egalitarianism.worryless <- V162244
egalitarianism.worryless[egalitarianism.worryless < 1] <- NA
egalitarianism.worryless[egalitarianism.worryless > 5] <- NA
egalitarianism.worryless <- (-1 * egalitarianism.worryless) + 6
#
egalitarianism.notbigprob <- V162245
egalitarianism.notbigprob[egalitarianism.notbigprob < 1] <- NA
egalitarianism.notbigprob[egalitarianism.notbigprob > 5] <- NA
egalitarianism.notbigprob <- (-1 * egalitarianism.notbigprob) + 6
#
egalitarianism.fewerprobs <- V162246
egalitarianism.fewerprobs[egalitarianism.fewerprobs < 1] <- NA
egalitarianism.fewerprobs[egalitarianism.fewerprobs > 5] <- NA
#
egalitarianism.matrix <- cbind(egalitarianism.equal,egalitarianism.worryless,
  egalitarianism.notbigprob,egalitarianism.fewerprobs)
egalitarianism.scores <- rowMeans(egalitarianism.matrix, na.rm=TRUE)
egalitarianism.scores[is.na(egalitarianism.scores)] <- NA
#
egalitarianism.scores <- egalitarianism.scores[nresponses >=3]
egalitarianism.scores2016 <- egalitarianism.scores
#
#  VALUES --- AUTHORITARIANISM
#
authoritarianism.independence <- V162239
authoritarianism.independence[authoritarianism.independence < 1] <- NA
authoritarianism.independence[authoritarianism.independence > 2] <- NA
#
authoritarianism.curiosity <- V162240
authoritarianism.curiosity[authoritarianism.curiosity < 1] <- NA
authoritarianism.curiosity[authoritarianism.curiosity > 2] <- NA
#
authoritarianism.obedience <- V162241
authoritarianism.obedience[authoritarianism.obedience < 1] <- NA
authoritarianism.obedience[authoritarianism.obedience > 2] <- NA
authoritarianism.obedience <- (-1 * authoritarianism.obedience) + 3
#
authoritarianism.considerate <- V162242
authoritarianism.considerate[authoritarianism.considerate < 1] <- NA
authoritarianism.considerate[authoritarianism.considerate > 2] <- NA
#
authoritarianism.matrix <- cbind(authoritarianism.independence,authoritarianism.curiosity,
  authoritarianism.obedience,authoritarianism.considerate)
authoritarianism.scores <- rowMeans(authoritarianism.matrix, na.rm=TRUE)
authoritarianism.scores[is.na(authoritarianism.scores)] <- NA
#
authoritarianism.scores <- authoritarianism.scores[nresponses >=3]
authoritarianism.scores2016 <- authoritarianism.scores
#
#  VALUES --- ROLE OF GOVERNMENT
#
govtrole.lessmore <- V162185
govtrole.lessmore[govtrole.lessmore < 1] <- NA
govtrole.lessmore[govtrole.lessmore > 2] <- NA
govtrole.lessmore <- (-1 * govtrole.lessmore) + 3
#
govtrole.market <- V162184
govtrole.market[govtrole.market < 1] <- NA
govtrole.market[govtrole.market > 2] <- NA
#
govtrole.big <- V162183
govtrole.big[govtrole.big < 1] <- NA
govtrole.big[govtrole.big > 2] <- NA
govtrole.big <- (-1 * govtrole.big) + 3
#
govtrole.matrix <- cbind(govtrole.lessmore,govtrole.market,govtrole.big)
govtrole.scores <- rowMeans(govtrole.matrix, na.rm=TRUE)
govtrole.scores[is.na(govtrole.scores)] <- NA
#
govtrole.scores <- govtrole.scores[nresponses >=3]
govtrole.scores2016 <- govtrole.scores
#
detach(data)
#
#
#
dat <- rbind(issues1980,issues1984,issues1988,issues1992,issues1996,issues2000,issues2004,issues2008,issues2012,issues2016)
#
# Cut out therms
therms <- dat[,colnames(dat) %in% c("carter.rating_reagan.rating")]
dat <- dat[,!colnames(dat) %in% c("carter.rating_reagan.rating")]
#
correctplacements <- c(correctplacement1980,correctplacement1984,correctplacement1988,correctplacement1992,correctplacement1996,
			correctplacement2000,correctplacement2004,correctplacement2008,correctplacement2012,correctplacement2016)
#
race <- c(nonhispwhite1980,nonhispwhite1984,nonhispwhite1988,nonhispwhite1992,
    nonhispwhite1996,nonhispwhite2000,nonhispwhite2004,nonhispwhite2008,
    nonhispwhite2012,nonhispwhite2016)
white <- race
#
party7 <- c(partyid1980,partyid1984,partyid1988,partyid1992,partyid1996,
    partyid2000,partyid2004,partyid2008,partyid2012,partyid2016)
partisan <- party7
partisan[partisan <=3 | partisan >=5] <- 1
partisan[partisan!=1 | is.na(partisan)] <- 0
#
moraltraditionalism <- c(moraltrad.scores1980,moraltrad.scores1984,moraltrad.scores1988,moraltrad.scores1992,moraltrad.scores1996,moraltrad.scores2000,moraltrad.scores2004,moraltrad.scores2008,moraltrad.scores2012,moraltrad.scores2016)
egalitarianism <- c(egalitarianism.scores1980,egalitarianism.scores1984,egalitarianism.scores1988,egalitarianism.scores1992,egalitarianism.scores1996,egalitarianism.scores2000,egalitarianism.scores2004,egalitarianism.scores2008,egalitarianism.scores2012,egalitarianism.scores2016)
authoritarianism <- c(authoritarianism.scores1980,authoritarianism.scores1984,authoritarianism.scores1988,authoritarianism.scores1992,authoritarianism.scores1996,authoritarianism.scores2000,authoritarianism.scores2004,authoritarianism.scores2008,authoritarianism.scores2012,authoritarianism.scores2016)
govtrole <- c(govtrole.scores1980,govtrole.scores1984,govtrole.scores1988,govtrole.scores1992,govtrole.scores1996,govtrole.scores2000,govtrole.scores2004,govtrole.scores2008,govtrole.scores2012,govtrole.scores2016)
#
#
#
#
# COMBINE WITH CUMULATIVE DATA FILE
#
cdfdata <- read.dta("Dropbox/mass_constraint/analysis/ANES19802016cdf.dta",convert.factors = FALSE)
yearstudy <- cdfdata[,"VCF0004"]
cdfdata2 <- cdfdata[(yearstudy==1980 | yearstudy==1984 | yearstudy==1988 | yearstudy==1992 | yearstudy==1996 | yearstudy==2000 | yearstudy==2004 | yearstudy==2008 | yearstudy==2012 | yearstudy==2016),]
#
combvector <- c(nresponses1980,nresponses1984,nresponses1988,nresponses1992,nresponses1996,nresponses2000,nresponses2004,nresponses2008,nresponses2012,nresponses2016)
cdfdata3 <- cdfdata2[combvector,]
#
attach(cdfdata3, warn.conflicts = FALSE)
#
# Political interest [0 - 2]
#
interest <- VCF0310
interest[interest < 1 | interest > 3] <- NA
interest <- interest - 1
#
# Political involvement [0 - 5]
#
involvement <- VCF0723
involvement[involvement < 1 | involvement > 6] <- NA
involvement <- involvement - 1
#
# Political knowledge [0 - 4]
#
knowledge <- VCF0050a
knowledge[knowledge < 1 | knowledge > 5] <- NA
knowledge <- (-1 * knowledge) + 6
knowledge <- knowledge - 1
#
# Political sophistication
#
sophistication.add <- interest + involvement + knowledge
#
sophistication.mean01 <- rowMeans(cbind((interest/2),(involvement/5),(knowledge/4)), na.rm=TRUE)
#
std.interest <- (interest - mean(interest, na.rm=TRUE)) / sd(interest, na.rm=TRUE)
std.involvement <- (involvement - mean(involvement, na.rm=TRUE)) / sd(involvement, na.rm=TRUE)
std.knowledge <- (knowledge - mean(knowledge, na.rm=TRUE)) / sd(knowledge, na.rm=TRUE)
#
psych::alpha(cbind(std.interest,std.involvement,std.knowledge))
#
sophistication.meanstd <- rowMeans(cbind(std.interest,std.involvement,std.knowledge), na.rm=TRUE)
#
sophistication.year <- VCF0004
#
sophistication.tertile1980 <- ntile(sophistication.meanstd[sophistication.year==1980], 3)
sophistication.tertile1984 <- ntile(sophistication.meanstd[sophistication.year==1984], 3)
sophistication.tertile1988 <- ntile(sophistication.meanstd[sophistication.year==1988], 3)
sophistication.tertile1992 <- ntile(sophistication.meanstd[sophistication.year==1992], 3)
sophistication.tertile1996 <- ntile(sophistication.meanstd[sophistication.year==1996], 3)
sophistication.tertile2000 <- ntile(sophistication.meanstd[sophistication.year==2000], 3)
sophistication.tertile2004 <- ntile(sophistication.meanstd[sophistication.year==2004], 3)
sophistication.tertile2008 <- ntile(sophistication.meanstd[sophistication.year==2008], 3)
sophistication.tertile2012 <- ntile(sophistication.meanstd[sophistication.year==2012], 3)
sophistication.tertile2016 <- ntile(sophistication.meanstd[sophistication.year==2016], 3)
#
sophistication.tertile <- c(sophistication.tertile1980,sophistication.tertile1984,sophistication.tertile1988,
	sophistication.tertile1992,sophistication.tertile1996,sophistication.tertile2000,sophistication.tertile2004,
    sophistication.tertile2008,sophistication.tertile2012,sophistication.tertile2016)
#
sophis <- sophistication.tertile
sophis2 <- sophis
#
detach(cdfdata3)
#
sophis <- sophis[!is.na(sophis2)]
dat <- dat[!is.na(sophis2),]
#
#
#
#
#   II. RUN DO-IRT MODEL WITH JAGS
#
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# junk (to deal with computing limitations)
#dat2 <- rbind(dat[1:200,],dat[1000:1200,],dat[2000:2200,],dat[3000:3200,],dat[4000:4200,],dat[5000:5200,],
#	dat[6000:6200,],dat[7000:7200,],dat[8000:8200,],dat[9000:9200,],dat[10000:10200,],dat[11000:11200,],
#	dat[12000:12200,],dat[13000:13200,],dat[14000:14200,],dat[15000:15200,],dat[16000:16200,],
#	dat[17000:17200,],dat[18000:18200,],dat[19000:19200,])
#dat <- dat2
#sophis <- sophistication.tertile
#sophis2 <- c(sophis[1:200],sophis[1000:1200],sophis[2000:2200],sophis[3000:3200],sophis[4000:4200],sophis[5000:5200],
#	sophis[6000:6200],sophis[7000:7200],sophis[8000:8200],sophis[9000:9200],sophis[10000:10200],sophis[11000:11200],
#	sophis[12000:12200],sophis[13000:13200],sophis[14000:14200],sophis[15000:15200],sophis[16000:16200],
#	sophis[17000:17200],sophis[18000:18200],sophis[19000:19200])
#sophis <- sophis2
#partisan2 <- c(partisan[1:100],partisan[1000:1100],partisan[2000:2100],partisan[3000:3100],partisan[4000:4100],
#	partisan[5000:5100],partisan[6000:6100],partisan[7000:7100],partisan[8000:8100],partisan[9000:9100],
#	partisan[10000:10100],partisan[11000:11100],partisan[12000:12100],partisan[13000:13100],partisan[14000:14100],
#	partisan[15000:15100],partisan[16000:16100],partisan[17000:17100],partisan[18000:18100],partisan[19000:19100])
#partisan <- partisan2
#white2 <- c(white[1:100],white[1000:1100],white[2000:2100],white[3000:3100],white[4000:4100],white[5000:5100],
#	white[6000:6100],white[7000:7100],white[8000:8100],white[9000:9100],white[10000:10100],white[11000:11100],
#	white[12000:12100],white[13000:13100],white[14000:14100],white[15000:15100],white[16000:16100],white[17000:17100],
#	white[18000:18100],white[19000:19100])
#white <- white2
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
Y <- dat[,4:ncol(dat)]
Y <- as.matrix(Y)
#
time <- dat[,3]
#
n <- nrow(Y)
p <- ncol(Y)
T <- max(time)
K <- apply(Y,2,max,na.rm=TRUE)
#
firs1980 <- 1
last1980 <- table(time)[1]
firs1984 <- last1980 + 1
last1984 <- last1980 + table(time)[2]
firs1988 <- last1984 + 1
last1988 <- last1984 + table(time)[3]
firs1992 <- last1988 + 1
last1992 <- last1988 + table(time)[4]
firs1996 <- last1992 + 1
last1996 <- last1992 + table(time)[5]
firs2000 <- last1996 + 1
last2000 <- last1996 + table(time)[6]
firs2004 <- last2000 + 1
last2004 <- last2000 + table(time)[7]
firs2008 <- last2004 + 1
last2008 <- last2004 + table(time)[8]
firs2012 <- last2008 + 1
last2012 <- last2008 + table(time)[9]
firs2016 <- last2012 + 1
last2016 <- last2012 + table(time)[10]
#
recorded.responses <- 29 - rowSums(is.na(Y))
table(recorded.responses)
#
#pdf("Dropbox/mass_constraint/images/DOIRT_recordedresponses.pdf", height=5.5, width=6.5)
#
ggplot(data.frame(recorded.responses), aes(x=recorded.responses)) +
    geom_histogram(binwidth=1, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
    stat_ecdf(geom = "step") +
    ggtitle("American National Election Studies: 1980-2016") +
    labs(subtitle="Number of issue responses\n", caption="\nN = 24,059") +
    ylab("Count\n") +
    xlab("\nResponses") +
    #theme_ipsum() +
    theme(plot.title=element_text(hjust = 0.5, size=15), plot.subtitle=element_text(hjust = 0.5), legend.position="top", legend.box="horizontal", legend.title=element_blank())
# extrafont::font_import()
# extrafont::loadfonts(quiet=TRUE)
# IBMPlexMono/IBMPlexSans
# import_plex_sans
# import_roboto_condensed()
dev.off()
#
#pdf("Dropbox/mass_constraint/images/DOIRT_recordedresponses_cdf.pdf", height=5.5, width=6.5)
#
ggplot(data.frame(recorded.responses), aes(x=recorded.responses)) +
    stat_ecdf(geom = "step") +
    ggtitle("American National Election Studies: 1980-2016") +
    labs(subtitle="Number of issue responses\n", caption="\nN = 24,059") +
    ylab("Cumulative density\n") +
    xlab("\nResponses") +
    theme(plot.title=element_text(hjust = 0.5, size=15), plot.subtitle=element_text(hjust = 0.5), legend.position="top", legend.box="horizontal", legend.title=element_blank())
#
dev.off()
#
#
#
doirt.model <- function() {
# ABORTION (COLUMN 1) 4-POINT
#
for (i in 1:n){
for (j in 1:1){
#
Y[i, j] ~ dcat(Pi[i, j, 1:4])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- 1 - Z[i, j, 3]
#
}}
#
# GAY DISCRIMINATION (COLUMN 2) 4-POINT
#
for (i in firs1988:n){
for (j in 2:2){
#
Y[i, j] ~ dcat(Pi[i, j, 1:4])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- 1 - Z[i, j, 3]
#
}}
#
# GAY ADOPTION (COLUMN 3) 2-POINT
#
for (i in firs2000:n){
for (j in 3:3){
#
Y[i, j] ~ dcat(Pi[i, j, 1:2])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- 1 - Z[i, j, 1]
#
}}
#
# GAY ADOPTION (COLUMN 4) 4-POINT
#
for (i in firs1992:last1992){
for (j in 4:4){
#
Y[i, j] ~ dcat(Pi[i, j, 1:4])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- 1 - Z[i, j, 3]
#
}}
#
# GAY MARRIAGE (COLUMN 5) 3-POINT
#
for (i in firs2004:n){
for (j in 5:5){
#
Y[i, j] ~ dcat(Pi[i, j, 1:3])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- 1 - Z[i, j, 2]
#
}}
#
# GUN CONTROL (COLUMN 6) 2-POINT
#
for (i in firs1996:last1996){
for (j in 6:6){
#
Y[i, j] ~ dcat(Pi[i, j, 1:2])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- 1 - Z[i, j, 1]
#
}}
#
# GUN CONTROL (COLUMN 7) 3-POINT
#
for (i in firs2008:n){
for (j in 7:7){
#
Y[i, j] ~ dcat(Pi[i, j, 1:3])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- 1 - Z[i, j, 2]
#
}}
#
# GUN CONTROL (COLUMN 8) 5-POINT
#
for (i in firs2000:last2004){
for (j in 8:8){
#
Y[i, j] ~ dcat(Pi[i, j, 1:5])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- 1 - Z[i, j, 4]
#
}}
#
# WELFARE AND POOR SPENDING (COLUMNS 9-10) 3-POINT
#
for (i in firs1992:n){
for (j in 9:10){
#
Y[i, j] ~ dcat(Pi[i, j, 1:3])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- 1 - Z[i, j, 2]
#
}}
#
# LIBCON, GOVTSPEND, AND GUARJOBS
# (COLUMNS 11-13) 7-POINT
#
for (i in 1:n){
for (j in 11:13){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# HEALTH INSURANCE (COLUMN 14) 7-POINT
#
for (i in firs1984:n){
for (j in 14:14){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# ENVIRONMENTJOBS (COLUMN 15) 7-POINT
#
for (i in firs1996:n){
for (j in 15:15){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# AID TO BLACKS AND DEFENSE SPENDING
# (COLUMNS 16-17) 7-POINT
#
for (i in 1:n){
for (j in 16:17){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# IMMIGRATION LEVEL (COLUMN 18) 5-POINT
#
for (i in firs1992:n){
for (j in 18:18){
#
Y[i, j] ~ dcat(Pi[i, j, 1:5])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- 1 - Z[i, j, 4]
#
}}
#
# LIBCON BRANCH (2000 ONLY)
# (COLUMN 19) 7-POINT
#
for (i in firs2000:last2000){
for (j in 19:19){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# GOVTSPEND - DEF SPEND BRANCH (2000 ONLY)
# (COLUMNS 20-25) 5-POINT
#
for (i in firs2000:last2000){
for (j in 20:25){
#
Y[i, j] ~ dcat(Pi[i, j, 1:5])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- 1 - Z[i, j, 4]
#
}}
#
# GOVTSPEND - DEFSPEND NEW (2008 ONLY)
# (COLUMNS 26-28) 7-POINT
#
for (i in firs2008:last2008){
for (j in 26:p){
#
Y[i, j] ~ dcat(Pi[i, j, 1:7])
probit(Z[i, j, 1]) <- alpha[j, 1, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 2]) <- alpha[j, 2, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 3]) <- alpha[j, 3, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 4]) <- alpha[j, 4, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 5]) <- alpha[j, 5, time[i]] - beta[j, time[i]]*x[i]
probit(Z[i, j, 6]) <- alpha[j, 6, time[i]] - beta[j, time[i]]*x[i]
#
Pi[i, j, 1] <- Z[i, j, 1]
Pi[i, j, 2] <- Z[i, j, 2] - Z[i, j, 1]
Pi[i, j, 3] <- Z[i, j, 3] - Z[i, j, 2]
Pi[i, j, 4] <- Z[i, j, 4] - Z[i, j, 3]
Pi[i, j, 5] <- Z[i, j, 5] - Z[i, j, 4]
Pi[i, j, 6] <- Z[i, j, 6] - Z[i, j, 5]
Pi[i, j, 7] <- 1 - Z[i, j, 6]
#
}}
#
# PRIORS ON X
#
for (i in 1:n){
x[i] ~ dnorm(0, 1)
}
#
# PRIORS ON BETA
# CONSTRAIN B11 (LIBCON) TO BE POSITIVE,
# B12 (GOVTSPEND) TO BE NEGATIVE
# ACROSS YEARS
#
for(j in 1:10){
beta[j,1] ~ dnorm(mu.beta.one, tau.beta.one)
for(t in 2:T){
beta[j,t] ~ dnorm(beta[j,t-1], tau.beta)
}}
#
for(j in 11:11){
beta[j,1] ~ dnorm(mu.beta.one, tau.beta.one);T(0,)
for(t in 2:T){
beta[j,t]~dnorm(beta[j,t-1], tau.beta);T(0,)
}}
#
for(j in 12:12){
beta[j,1] ~ dnorm(mu.beta.one, tau.beta.one);T(,0)
for(t in 2:T){
beta[j,t]~dnorm(beta[j,t-1], tau.beta);T(,0)
}}
#
for(j in 13:p){
beta[j,1] ~ dnorm(mu.beta.one, tau.beta.one)
for(t in 2:T){
beta[j,t]~dnorm(beta[j,t-1], tau.beta)
}}
#
# PRIORS ON ALPHA
#
# NON TWO-POINT SCALES
for (j in 1:2){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (c in K[j]:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
for (c in K[j]:6){
alpha[j, c, t] <- 0
}
}}
#
# TWO-POINT SCALES
for (j in 3:3){
alphastar[j, 1, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
alpha[j, 1, 1] <- alphastar[j, 1, 1]
for (c in 2:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
alphastar[j, 1, t] ~ dnorm(alphastar[j, 1, (t-1)], tau.alpha)
alpha[j, 1, t] <- alphastar[j, 1, t]
for (c in 2:6){
alpha[j, c, t] <- 0
}
}}
#
for (j in 4:5){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (c in K[j]:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
for (c in K[j]:6){
alpha[j, c, t] <- 0
}
}}
#
for (j in 6:6){
alphastar[j, 1, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
alpha[j, 1, 1] <- alphastar[j, 1, 1]
for (c in 2:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
alphastar[j, 1, t] ~ dnorm(alphastar[j, 1, (t-1)], tau.alpha)
alpha[j, 1, t] <- alphastar[j, 1, t]
for (c in 2:6){
alpha[j, c, t] <- 0
}
}}
#
for (j in 7:10){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (c in K[j]:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
for (c in K[j]:6){
alpha[j, c, t] <- 0
}
}}
#
## SEVEN-POINT SCALES
for (j in 11:17){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
}}
#
# NON TWO-POINT SCALES
for (j in 18:18){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (c in K[j]:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
for (c in K[j]:6){
alpha[j, c, t] <- 0
}
}}
#
## SEVEN-POINT SCALES
for (j in 19:19){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
}}
#
# NON TWO-POINT SCALES
for (j in 20:25){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (c in K[j]:6){
alpha[j, c, 1] <- 0
}
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
for (c in K[j]:6){
alpha[j, c, t] <- 0
}
}}
#
## SEVEN-POINT SCALES
for (j in 26:p){
for (c in 1:(K[j]-1)){
alphastar[j, c, 1] ~ dnorm(mu.alpha.one, tau.alpha.one)
}
alpha[j, 1:(K[j]-1), 1] <- sort(alphastar[j,1:(K[j]-1),1])
for (t in 2:T){
for (c in 1:(K[j]-1)){
alphastar[j, c, t] ~ dnorm(alphastar[j,c,(t-1)], tau.alpha)
}
alpha[j,1:(K[j]-1),t] <- sort(alphastar[j,1:(K[j]-1),t])
}}
#
# HYPER PRIORS ON MEAN AND PRECISION TERMS
#
tau.alpha ~ dgamma(1, 0.1)
tau.beta ~ dgamma(1, 0.1)
mu.beta.one ~ dnorm(0, 1)
tau.beta.one ~ dgamma(1, 0.1)
mu.alpha.one ~ dnorm(0, 1)
tau.alpha.one ~ dgamma(1, 0.1)
#
}
#
# !!!!!!!!!!!!!!!!!!!
# !!!!!! emIRT !!!!!!
# !!!!!!!!!!!!!!!!!!!
#
YYY <- Y[,1]
YYYY <- Y[,11:16]
#
YYY[YYY==1] <- 1
YYY[YYY==2] <- 2
YYY[YYY==3] <- 2
YYY[YYY==4] <- 3
YYY[is.na(YYY)] <- 0
#
YYYY[YYYY<=3] <- 1
YYYY[YYYY==4] <- 2
YYYY[YYYY>=5] <- 3
YYYY[is.na(YYYY)] <- 0
#
newscales <- cbind(YYY,YYYY)
#
## Generate starts and priors
NN <- nrow(newscales)
JJ <- ncol(newscales)
cur <- vector(mode = "list")
cur$DD <- matrix(rep(0.5,JJ), ncol=1)
cur$tau <- matrix(rep(-0.5,JJ), ncol=1)
cur$beta <- matrix(runif(JJ,0,1), ncol=1)
cur$x <- matrix(runif(NN,-1,1), ncol=1)
priors <- vector(mode = "list")
priors$x <- list(mu = matrix(0,1,1), sigma = matrix(1,1,1) )
priors$beta <- list(mu = matrix(0,2,1), sigma = matrix(diag(25,2),2,2))
#
res <- ordIRT(.rc = newscales, .starts=cur, .priors=priors, .D = 1,
.control = {list(verbose = TRUE,
thresh = 1e-6, maxit = 5000)})
#
em.irt.idealpoints <- as.vector(res$means$x) * 0.932
#
if (cor(dat[,"guarjobs"], em.irt.idealpoints, use="complete") < 0) {em.irt.idealpoints <- -1 * em.irt.idealpoints}
#
# !!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!!!!!!!
#
alphastar.generator <- function(p.=p, T.=T){
    alphastar.mat <- list(NA)
      for (t in 1:T.){
        alphastar.mat[[t]] <- t(apply(matrix(rnorm(p.*6), nrow=p., ncol=6), 1, function(x) x <- sort(x)))
        alphastar.mat[[t]][1,4:6] <- NA
        alphastar.mat[[t]][2,4:6] <- NA
        alphastar.mat[[t]][3,2:6] <- NA
        alphastar.mat[[t]][4,4:6] <- NA
        alphastar.mat[[t]][5,3:6] <- NA
        alphastar.mat[[t]][6,2:6] <- NA
        alphastar.mat[[t]][7,3:6] <- NA
        alphastar.mat[[t]][8,5:6] <- NA
        alphastar.mat[[t]][9,3:6] <- NA
        alphastar.mat[[t]][10,3:6] <- NA
        alphastar.mat[[t]][18,5:6] <- NA
        alphastar.mat[[t]][20,5:6] <- NA
        alphastar.mat[[t]][21,5:6] <- NA
        alphastar.mat[[t]][22,5:6] <- NA
        alphastar.mat[[t]][23,5:6] <- NA
        alphastar.mat[[t]][24,5:6] <- NA
        alphastar.mat[[t]][25,5:6] <- NA
      }
  alphastar <- array(unlist(alphastar.mat), c(p., 6, T))
  return(alphastar)
}
#
beta.generator <- function(p.=p, T.=T){
    beta <- matrix(rnorm(p.*T.), nrow=p., ncol=T.)
    beta[1,] <- -1 * abs(beta[1,])
    beta[2,] <- abs(beta[2,])
    beta[3,] <- abs(beta[3,])
    beta[4,] <- abs(beta[4,])
    beta[5,] <- abs(beta[5,])
    beta[6,] <- abs(beta[6,])
    beta[7,] <- abs(beta[7,])
    beta[8,] <- abs(beta[8,])
    beta[9,] <- abs(beta[9,])
    beta[10,] <- abs(beta[10,])
    beta[11,] <- abs(beta[11,])
    beta[12,] <- -1 * abs(beta[12,])
    beta[13,] <- abs(beta[13,])
    beta[14,] <- abs(beta[14,])
    beta[15,] <- abs(beta[15,])
    beta[16,] <- abs(beta[16,])
    beta[17,] <- abs(beta[17,])
    beta[18,] <- abs(beta[18,])
    beta[19,] <- abs(beta[19,])
    beta[20,] <- -1 * abs(beta[20,])
    beta[21,] <- abs(beta[21,])
    beta[22,] <- abs(beta[22,])
    beta[23,] <- abs(beta[23,])
    beta[24,] <- abs(beta[24,])
    beta[25,] <- abs(beta[25,])
    beta[26,] <- abs(beta[26,])
    beta[27,] <- abs(beta[27,])
    beta[28,] <- abs(beta[28,])
    return(beta)
}
#
inits <- function(){
  list(alphastar = alphastar.generator(),
       beta = beta.generator(),
       x = em.irt.idealpoints + rnorm(n,0,0.05))
}
#
# !!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!!!!!!!
#
# BAYESIAN ORDINAL IRT EXPERIMENTS
#
# Overall (fully static)
BayesianOIRT.all <- MCMCordfactanal(Y, factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
# Static, but split one issue at a time across periods
masterY <- list(NA)
for (j in 1:18){
selectedissue <- Y[,j]
newmat <- matrix(NA, nrow=n, ncol=T)
colnames(newmat) <- paste(colnames(Y)[j], "_t", 1:T, sep="")
  for (t in 1:T){
    newmat[time==t,t] <- selectedissue[time==t]
    }
masterY[[j]] <- as.matrix(data.frame(Y[,-j], newmat))
}
#
BayesianOIRTsamps.abortion <- MCMCordfactanal(masterY[[1]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.abortion <- data.frame(mean=summary(BayesianOIRTsamps.abortion)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.abortion)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.abortion)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.abortion <- BayesianOIRTsummary.abortion[grepl("Lambdaabortion", rownames(BayesianOIRTsummary.abortion)),]
discriminationparams.abortion <- BayesianOIRTsummary2.abortion[seq(2,nrow(BayesianOIRTsummary2.abortion),by=2),]
#
#
BayesianOIRTsamps.gaydiscrimination <- MCMCordfactanal(masterY[[2]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.gaydiscrimination <- data.frame(mean=summary(BayesianOIRTsamps.gaydiscrimination)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.gaydiscrimination)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.gaydiscrimination)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.gaydiscrimination <- BayesianOIRTsummary.gaydiscrimination[grepl("Lambdagaydiscrimination", rownames(BayesianOIRTsummary.gaydiscrimination)),]
discriminationparams.gaydiscrimination <- BayesianOIRTsummary2.gaydiscrimination[seq(2,nrow(BayesianOIRTsummary2.gaydiscrimination),by=2),]
#
#
BayesianOIRTsamps.gayadoption2pt <- MCMCordfactanal(masterY[[3]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.gayadoption2pt <- data.frame(mean=summary(BayesianOIRTsamps.gayadoption2pt)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.gayadoption2pt)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.gayadoption2pt)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.gayadoption2pt <- BayesianOIRTsummary.gayadoption2pt[grepl("Lambdagayadoption_2pt", rownames(BayesianOIRTsummary.gayadoption2pt)),]
discriminationparams.gayadoption2pt <- BayesianOIRTsummary2.gayadoption2pt[seq(2,nrow(BayesianOIRTsummary2.gayadoption2pt),by=2),]
#
#
BayesianOIRTsamps.gayadoption4pt <- MCMCordfactanal(masterY[[4]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.gayadoption4pt <- data.frame(mean=summary(BayesianOIRTsamps.gayadoption4pt)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.gayadoption4pt)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.gayadoption4pt)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.gayadoption4pt <- BayesianOIRTsummary.gayadoption4pt[grepl("Lambdagayadoption_4pt", rownames(BayesianOIRTsummary.gayadoption4pt)),]
discriminationparams.gayadoption4pt <- BayesianOIRTsummary2.gayadoption4pt[seq(2,nrow(BayesianOIRTsummary2.gayadoption4pt),by=2),]
#
#
BayesianOIRTsamps.gaymarriage <- MCMCordfactanal(masterY[[5]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.gaymarriage <- data.frame(mean=summary(BayesianOIRTsamps.gaymarriage)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.gaymarriage)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.gaymarriage)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.gaymarriage <- BayesianOIRTsummary.gaymarriage[grepl("Lambdagaymarriage", rownames(BayesianOIRTsummary.gaymarriage)),]
discriminationparams.gaymarriage <- BayesianOIRTsummary2.gaymarriage[seq(2,nrow(BayesianOIRTsummary2.gaymarriage),by=2),]
#
#
BayesianOIRTsamps.guncontrol2pt <- MCMCordfactanal(masterY[[6]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.guncontrol2pt <- data.frame(mean=summary(BayesianOIRTsamps.guncontrol2pt)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.guncontrol2pt)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.guncontrol2pt)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.guncontrol2pt <- BayesianOIRTsummary.guncontrol2pt[grepl("Lambdaguncontrol_2pt", rownames(BayesianOIRTsummary.guncontrol2pt)),]
discriminationparams.guncontrol2pt <- BayesianOIRTsummary2.guncontrol2pt[seq(2,nrow(BayesianOIRTsummary2.guncontrol2pt),by=2),]
#
#
BayesianOIRTsamps.guncontrol3pt <- MCMCordfactanal(masterY[[7]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.guncontrol3pt <- data.frame(mean=summary(BayesianOIRTsamps.guncontrol3pt)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.guncontrol3pt)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.guncontrol3pt)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.guncontrol3pt <- BayesianOIRTsummary.guncontrol3pt[grepl("Lambdaguncontrol_3pt", rownames(BayesianOIRTsummary.guncontrol3pt)),]
discriminationparams.guncontrol3pt <- BayesianOIRTsummary2.guncontrol3pt[seq(2,nrow(BayesianOIRTsummary2.guncontrol3pt),by=2),]
#
#
BayesianOIRTsamps.guncontrol5pt <- MCMCordfactanal(masterY[[8]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.guncontrol5pt <- data.frame(mean=summary(BayesianOIRTsamps.guncontrol5pt)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.guncontrol5pt)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.guncontrol5pt)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.guncontrol5pt <- BayesianOIRTsummary.guncontrol5pt[grepl("Lambdaguncontrol_5pt", rownames(BayesianOIRTsummary.guncontrol5pt)),]
discriminationparams.guncontrol5pt <- BayesianOIRTsummary2.guncontrol5pt[seq(2,nrow(BayesianOIRTsummary2.guncontrol5pt),by=2),]
#
#
BayesianOIRTsamps.welfarespend <- MCMCordfactanal(masterY[[9]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.welfarespend <- data.frame(mean=summary(BayesianOIRTsamps.welfarespend)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.welfarespend)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.welfarespend)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.welfarespend <- BayesianOIRTsummary.welfarespend[grepl("Lambdawelfarespend", rownames(BayesianOIRTsummary.welfarespend)),]
discriminationparams.welfarespend <- BayesianOIRTsummary2.welfarespend[seq(2,nrow(BayesianOIRTsummary2.welfarespend),by=2),]
#
#
BayesianOIRTsamps.poorspend <- MCMCordfactanal(masterY[[10]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.poorspend <- data.frame(mean=summary(BayesianOIRTsamps.poorspend)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.poorspend)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.poorspend)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.poorspend <- BayesianOIRTsummary.poorspend[grepl("Lambdapoorspend", rownames(BayesianOIRTsummary.poorspend)),]
discriminationparams.poorspend <- BayesianOIRTsummary2.poorspend[seq(2,nrow(BayesianOIRTsummary2.poorspend),by=2),]
#
#
BayesianOIRTsamps.libcon <- MCMCordfactanal(masterY[[11]], factors=1,
	lambda.constraints=list(guarjobs=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.libcon <- data.frame(mean=summary(BayesianOIRTsamps.libcon)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.libcon)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.libcon)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.libcon <- BayesianOIRTsummary.libcon[grepl("Lambdalibcon_", rownames(BayesianOIRTsummary.libcon)),]
discriminationparams.libcon <- BayesianOIRTsummary2.libcon[seq(2,nrow(BayesianOIRTsummary2.libcon),by=2),]
#
#
BayesianOIRTsamps.govtservices <- MCMCordfactanal(masterY[[12]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), abortion.scale=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.govtservices <- data.frame(mean=summary(BayesianOIRTsamps.govtservices)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.govtservices)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.govtservices)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.govtservices <- BayesianOIRTsummary.govtservices[grepl("Lambdagovtservices_", rownames(BayesianOIRTsummary.govtservices)),]
discriminationparams.govtservices <- BayesianOIRTsummary2.govtservices[seq(2,nrow(BayesianOIRTsummary2.govtservices),by=2),]
#
#
BayesianOIRTsamps.guarjobs <- MCMCordfactanal(masterY[[13]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.guarjobs <- data.frame(mean=summary(BayesianOIRTsamps.guarjobs)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.guarjobs)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.guarjobs)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.guarjobs <- BayesianOIRTsummary.guarjobs[grepl("Lambdaguarjobs_", rownames(BayesianOIRTsummary.guarjobs)),]
discriminationparams.guarjobs <- BayesianOIRTsummary2.guarjobs[seq(2,nrow(BayesianOIRTsummary2.guarjobs),by=2),]
#
#
BayesianOIRTsamps.healthinsurance <- MCMCordfactanal(masterY[[14]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.healthinsurance <- data.frame(mean=summary(BayesianOIRTsamps.healthinsurance)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.healthinsurance)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.healthinsurance)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.healthinsurance <- BayesianOIRTsummary.healthinsurance[grepl("Lambdahealthinsurance_", rownames(BayesianOIRTsummary.healthinsurance)),]
discriminationparams.healthinsurance <- BayesianOIRTsummary2.healthinsurance[seq(2,nrow(BayesianOIRTsummary2.healthinsurance),by=2),]
#
#
BayesianOIRTsamps.environmentjobs <- MCMCordfactanal(masterY[[15]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.environmentjobs <- data.frame(mean=summary(BayesianOIRTsamps.environmentjobs)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.environmentjobs)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.environmentjobs)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.environmentjobs <- BayesianOIRTsummary.environmentjobs[grepl("Lambdaenvironmentjobs_", rownames(BayesianOIRTsummary.environmentjobs)),]
discriminationparams.environmentjobs <- BayesianOIRTsummary2.environmentjobs[seq(2,nrow(BayesianOIRTsummary2.environmentjobs),by=2),]
#
#
BayesianOIRTsamps.aidtoblacks <- MCMCordfactanal(masterY[[16]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.aidtoblacks <- data.frame(mean=summary(BayesianOIRTsamps.aidtoblacks)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.aidtoblacks)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.aidtoblacks)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.aidtoblacks <- BayesianOIRTsummary.aidtoblacks[grepl("Lambdaaidtoblacks_", rownames(BayesianOIRTsummary.aidtoblacks)),]
discriminationparams.aidtoblacks <- BayesianOIRTsummary2.aidtoblacks[seq(2,nrow(BayesianOIRTsummary2.aidtoblacks),by=2),]
#
#
BayesianOIRTsamps.defensespending <- MCMCordfactanal(masterY[[17]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.defensespending <- data.frame(mean=summary(BayesianOIRTsamps.defensespending)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.defensespending)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.defensespending)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.defensespending <- BayesianOIRTsummary.defensespending[grepl("Lambdadefensespending_", rownames(BayesianOIRTsummary.defensespending)),]
discriminationparams.defensespending <- BayesianOIRTsummary2.defensespending[seq(2,nrow(BayesianOIRTsummary2.defensespending),by=2),]
#
#
BayesianOIRTsamps.immigrationlevel <- MCMCordfactanal(masterY[[18]], factors=1,
	lambda.constraints=list(libcon=list(2,"+"), govtservices=list(2,"-")),
	burnin=2000, mcmc=1000, thin=5, verbose=3000,
	L0=0.5, store.lambda=TRUE, store.scores=FALSE)
#
BayesianOIRTsummary.immigrationlevel <- data.frame(mean=summary(BayesianOIRTsamps.immigrationlevel)[[1]][,"Mean"],
  lb=summary(BayesianOIRTsamps.immigrationlevel)[[2]][,"2.5%"],
  ub=summary(BayesianOIRTsamps.immigrationlevel)[[2]][,"97.5%"])
#
BayesianOIRTsummary2.immigrationlevel <- BayesianOIRTsummary.immigrationlevel[grepl("Lambdaimmigrationlevel", rownames(BayesianOIRTsummary.immigrationlevel)),]
discriminationparams.immigrationlevel <- BayesianOIRTsummary2.immigrationlevel[seq(2,nrow(BayesianOIRTsummary2.immigrationlevel),by=2),]
#
#
#
save(BayesianOIRT.all, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRT.all.Rda")
save(BayesianOIRTsamps.abortion, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.abortion.Rda")
save(BayesianOIRTsamps.gaydiscrimination, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gaydiscrimination.Rda")
save(BayesianOIRTsamps.gayadoption2pt, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gayadoption2pt.Rda")
save(BayesianOIRTsamps.gayadoption4pt, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gayadoption4pt.Rda")
save(BayesianOIRTsamps.gaymarriage, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gaymarriage.Rda")
save(BayesianOIRTsamps.guncontrol2pt, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol2pt.Rda")
save(BayesianOIRTsamps.guncontrol3pt, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol3pt.Rda")
save(BayesianOIRTsamps.guncontrol5pt, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol5pt.Rda")
save(BayesianOIRTsamps.welfarespend, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.welfarespend.Rda")
save(BayesianOIRTsamps.poorspend, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.poorspend.Rda")
save(BayesianOIRTsamps.libcon, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.libcon.Rda")
save(BayesianOIRTsamps.govtservices, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.govtservices.Rda")
save(BayesianOIRTsamps.guarjobs, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guarjobs.Rda")
save(BayesianOIRTsamps.healthinsurance, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.healthinsurance.Rda")
save(BayesianOIRTsamps.environmentjobs, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.environmentjobs.Rda")
save(BayesianOIRTsamps.aidtoblacks, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.aidtoblacks.Rda")
save(BayesianOIRTsamps.defensespending, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.defensespending.Rda")
save(BayesianOIRTsamps.immigrationlevel, file="Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.immigrationlevel.Rda")
#
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRT.all.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.abortion.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gaydiscrimination.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gayadoption2pt.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gayadoption4pt.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.gaymarriage.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol2pt.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol3pt.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guncontrol5pt.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.welfarespend.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.poorspend.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.libcon.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.govtservices.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.guarjobs.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.healthinsurance.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.environmentjobs.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.aidtoblacks.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.defensespending.Rda")
#load("Dropbox/mass_constraint/analysis/robustnesschecks/BayesianOIRTsamps.immigrationlevel.Rda")
#
#
#
# Betas
#
beta_libcon <- data.frame(cbind(issue=rep("Liberal-Conservative ID",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_libcon[,3:5] <- discriminationparams.libcon
beta_abortion <- data.frame(cbind(issue=rep("Abortion",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_abortion[,3:5] <- discriminationparams.abortion
beta_gaydisc <- data.frame(cbind(issue=rep("Gay Discrimination",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_gaydisc[3:10,3:5] <- discriminationparams.gaydiscrimination
beta_gayadopt <- data.frame(cbind(issue=rep("Gay Adoption",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_gayadopt[4,3:5] <- discriminationparams.gayadoption4pt
beta_gayadopt[6:10,3:5] <- discriminationparams.gayadoption2pt
beta_gaymarriage <- data.frame(cbind(issue=rep("Gay Marriage",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_gaymarriage[7:10,3:5] <- discriminationparams.gaymarriage
beta_guncontrol <- data.frame(cbind(issue=rep("Gun Control",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_guncontrol[5,3:5] <- discriminationparams.guncontrol2pt
beta_guncontrol[6:7,3:5] <- discriminationparams.guncontrol5pt
beta_guncontrol[8:10,3:5] <- discriminationparams.guncontrol3pt
beta_envjobs <- data.frame(cbind(issue=rep("Environment-Jobs",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_envjobs[5:10,3:5] <- discriminationparams.environmentjobs
beta_govtspend <- data.frame(cbind(issue=rep("Govt Spending and Services",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_govtspend[,3:5] <- discriminationparams.govtservices
beta_guarjobs <- data.frame(cbind(issue=rep("Guaranteed Jobs",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_guarjobs[,3:5] <- discriminationparams.guarjobs
beta_healthins <- data.frame(cbind(issue=rep("Health Insurance",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_healthins[2:10,3:5] <- discriminationparams.healthinsurance
beta_defspend <- data.frame(cbind(issue=rep("Defense Spending",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_defspend[,3:5] <- discriminationparams.defensespending
beta_aidblacks <- data.frame(cbind(issue=rep("Aid to Blacks",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_aidblacks[,3:5] <- discriminationparams.aidtoblacks
beta_welfarespend <- data.frame(cbind(issue=rep("Welfare Spending",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_welfarespend[4:10,3:5] <- discriminationparams.welfarespend
beta_poorspend <- data.frame(cbind(issue=rep("Poor Spending",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_poorspend[4:10,3:5] <- discriminationparams.poorspend
beta_immigration <- data.frame(cbind(issue=rep("Immigration",10), year=seq(1980,2016,by=4), mean=NA, lb=NA, ub=NA))
beta_immigration[4:10,3:5] <- discriminationparams.immigrationlevel
#
# Combine Betas
#
names(beta_libcon) <- names(beta_abortion) <- names(beta_gaydisc) <- names(beta_gayadopt) <- names(beta_gaymarriage) <- c("issue","year","mean","lb","ub")
names(beta_guncontrol) <- names(beta_envjobs) <- names(beta_govtspend) <- names(beta_guarjobs) <- names(beta_healthins) <- c("issue","year","mean","lb","ub")
names(beta_defspend) <- names(beta_aidblacks) <- names(beta_welfarespend) <- names(beta_poorspend) <- names(beta_immigration) <- c("issue","year","mean","lb","ub")
#
betamat <- rbind(beta_libcon, beta_abortion, beta_gaydisc, beta_gayadopt, beta_gaymarriage,
  beta_guncontrol, beta_envjobs, beta_govtspend, beta_guarjobs, beta_healthins,
  beta_defspend, beta_aidblacks, beta_welfarespend, beta_poorspend, beta_immigration)
#
names(betamat) <- c("issue","year","betapoint","betalow","betahigh")
#
betamat$year <- as.numeric(as.character(betamat$year))
betamat$betapoint <- as.numeric(as.character(betamat$betapoint))
betamat$betalow <- as.numeric(as.character(betamat$betalow))
betamat$betahigh <- as.numeric(as.character(betamat$betahigh))
#
# Make sure all discrimination parameters are positive (absolute values)
for (i in 1:nrow(betamat)){
    if (!is.na(betamat$betapoint[i]) & betamat$betapoint[i] < 0) {betamat[i,3:5] <- -1 * betamat[i,3:5]}
}
#
#
pdf("Dropbox/mass_constraint/images/OIRTexperiments_discparams.pdf", height=9.5, width=7.0)
#
ggplot(na.omit(betamat), aes(x=year, y=betapoint)) +
  geom_pointrange(aes(ymin = betalow, ymax = betahigh), fill='gray85', color='black', shape=21, fatten=2.5, size=0.65) +
  facet_wrap(~issue, nrow=5) +
  ylim(-0.1,1.3) +
  theme_bw() +
  xlab("\n Year") +
  ylab(expression(atop("Discrimination parameter (" *beta[jt] *")",""))) +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Parameter estimates from static ordinal IRT models",
    caption="\nBars show 95% credible intervals.") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
#
#
#samps <- foreach(i = 1:3, .packages = c('dclone', 'rjags'),
#              .combine = 'c', .final = mcmc.list) %dopar% {
#                DOIRT.sim <- jags.fit(data = list(Y=Y, time=time, n=n, p=p, T=T, K=K,
#				                      firs1984=firs1984, firs1988=firs1988,
#				                      firs1992=firs1992, last1992=last1992,
#				                      firs1996=firs1996, last1996=last1996,
#				                      firs2000=firs2000, last2000=last2000,
#				                      firs2004=firs2004, last2004=last2004,
#				                      firs2008=firs2008, last2008=last2008),
#			                      params = c("alpha", "beta", "x"),
#                            model = doirt.model,
#                            n.chains = 1,
#                            n.adapt = 1000,
#                            n.update = 1000,
#                            n.iter = 1000,
#                            thin = 5,
#                            inits = inits)
#                          }
#
# Write results to disk:
#save(samps, file="Dropbox/mass_constraint/analysis/DOIRT_ANES_1980-2016_samps.Rda")
#
load("Dropbox/mass_constraint/analysis/DOIRT_ANES_1980-2016_samps.Rda")
samps <- samps
#
#
#
#
#   MCMC DIAGNOSTICS
#
# Trace and Density Plots:
#
#windows()
#plot(samps[,paste("alpha[",sample(1:p,3),",1,",sample(1:T,3),"]",sep="")])
#windows()
#plot(samps[,paste("beta[",sample(1:p,3),",",sample(1:T,3),"]",sep="")])
#windows()
#plot(samps[,paste("x[",sample(1:n,3),"]",sep="")])
#
pdf("Dropbox/mass_constraint/images/DOIRT_convergence_traceplots.pdf", height=7.5, width=6.5)
#
set.seed(1985)
xtemp <- paste("x[",sample(1:n,5),"]",sep="")
mcmcplots::traplot(samps, parms=xtemp, regex = c("beta\\[1,","alpha\\[1,1,",xtemp), greek=TRUE)
#
dev.off()
#
# Autocorrelation Plots:
#
#windows()
#autocorr.plot(samps[,paste("alpha[",sample(1:p,4),",1,",sample(1:T,4),"]",sep="")])
#windows()
#autocorr.plot(samps[,paste("beta[",sample(1:p,4),",",sample(1:T,4),"]",sep="")])
#windows()
#autocorr.plot(samps[,paste("x[",sample(1:n,4),"]",sep="")])
#
# Geweke Diagnostic:
#
geweke_alpha <- geweke.diag(samps[,grep("alpha", colnames(samps[[1]]))])
geweke_alpha_values <- c(geweke_alpha[[1]]$z, geweke_alpha[[2]]$z)
geweke_alpha_values <- geweke_alpha_values[!is.na(geweke_alpha_values)]
#
length(geweke_alpha_values[abs(geweke_alpha_values) > 1.96]) / length(geweke_alpha_values)
#
geweke_beta <- geweke.diag(samps[,grep("beta", colnames(samps[[1]]))])
geweke_beta_values <- c(geweke_beta[[1]]$z, geweke_beta[[2]]$z, geweke_beta[[3]]$z)
geweke_beta_values <- geweke_beta_values[!is.na(geweke_beta_values)]
#
length(geweke_beta_values[abs(geweke_beta_values) > 1.96]) / length(geweke_beta_values)
#
geweke_x <- geweke.diag(samps[,grep("x", colnames(samps[[1]]))])
geweke_x_values <- c(geweke_x[[1]]$z, geweke_x[[2]]$z, geweke_x[[3]]$z)
geweke_x_values <- geweke_x_values[!is.na(geweke_x_values)]
#
length(geweke_x_values[abs(geweke_x_values) > 1.96]) / length(geweke_x_values)
#
# Gelman-Rubin Diagnostic:
#
#gelman.diag(samps[,grep("beta", colnames(samps[[1]]))])
#
#
#
#   SUMMARIZE DATA
#
alpha.samps <- samps[,grep("alpha", colnames(samps[[1]]))]
beta.samps <- samps[,grep("beta", colnames(samps[[1]]))]
x.samps <- samps[,grep("x", colnames(samps[[1]]))]
#
alpha.summary <- summary(alpha.samps)
beta.summary <- summary(beta.samps)
x.summary <- summary(x.samps)
#
alpha.pointestimates <- alpha.summary[[1]][,1]
beta.pointestimates <- beta.summary[[1]][,1]
x.idealpoints <- x.summary[[1]][,1]
#
#
#
# Response probabilities: abortion, libcon, govtservices, guarjobs
#
idealpoint.range <- quantile(x.idealpoints, probs=c(0.1, 0.25, 0.5, 0.75, 0.9))
#
idealpoint.10 <- quantile(x.idealpoints, probs=0.1)
idealpoint.25 <- quantile(x.idealpoints, probs=0.25)
idealpoint.50 <- quantile(x.idealpoints, probs=0.5)
idealpoint.75 <- quantile(x.idealpoints, probs=0.75)
idealpoint.90 <- quantile(x.idealpoints, probs=0.9)
#
responseprobs.Z <- array(NA, dim = c(10,5,6,28))
#
for (t in 1:10){
  for (f in 1:28){
  for (c in 1:6){
    for (i in 1:5){
    responseprobs.Z[t,i,c,f] <- pnorm(alpha.pointestimates[paste("alpha[",f,",",c,",",t,"]",sep="")] - (beta.pointestimates[paste("beta[",f,",",t,"]",sep="")] * idealpoint.range[i]))
}}}}
#
#
responseprobs.abortion.Pi1 <- data.frame(issue="abortion", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,1],
  ideal2=responseprobs.Z[,2,1,1],
  ideal3=responseprobs.Z[,3,1,1],
  ideal4=responseprobs.Z[,4,1,1],
  ideal5=responseprobs.Z[,5,1,1])
#
responseprobs.abortion.Pi2 <- data.frame(issue="abortion", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,1] - responseprobs.Z[,1,1,1],
  ideal2=responseprobs.Z[,2,2,1] - responseprobs.Z[,2,1,1],
  ideal3=responseprobs.Z[,3,2,1] - responseprobs.Z[,3,1,1],
  ideal4=responseprobs.Z[,4,2,1] - responseprobs.Z[,4,1,1],
  ideal5=responseprobs.Z[,5,2,1] - responseprobs.Z[,5,1,1])
#
responseprobs.abortion.Pi3 <- data.frame(issue="abortion", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,1] - responseprobs.Z[,1,2,1],
  ideal2=responseprobs.Z[,2,3,1] - responseprobs.Z[,2,2,1],
  ideal3=responseprobs.Z[,3,3,1] - responseprobs.Z[,3,2,1],
  ideal4=responseprobs.Z[,4,3,1] - responseprobs.Z[,4,2,1],
  ideal5=responseprobs.Z[,5,3,1] - responseprobs.Z[,5,2,1])
#
responseprobs.abortion.Pi4 <- data.frame(issue="abortion", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,3,1],
  ideal2=1 - responseprobs.Z[,2,3,1],
  ideal3=1 - responseprobs.Z[,3,3,1],
  ideal4=1 - responseprobs.Z[,4,3,1],
  ideal5=1 - responseprobs.Z[,5,3,1])
#
responseprobs.abortion.combined <- rbind(responseprobs.abortion.Pi1, responseprobs.abortion.Pi2, responseprobs.abortion.Pi3, responseprobs.abortion.Pi4)
#
names(responseprobs.abortion.combined)[which(names(responseprobs.abortion.combined)=="ideal1")] <- "10%"
names(responseprobs.abortion.combined)[which(names(responseprobs.abortion.combined)=="ideal2")] <- "25%"
names(responseprobs.abortion.combined)[which(names(responseprobs.abortion.combined)=="ideal3")] <- "50%"
names(responseprobs.abortion.combined)[which(names(responseprobs.abortion.combined)=="ideal4")] <- "75%"
names(responseprobs.abortion.combined)[which(names(responseprobs.abortion.combined)=="ideal5")] <- "90%"
#
responseprobs.abortion.combined$whichresponse[responseprobs.abortion.combined$whichresponse=="1"] <- "By law, abortion should never be permitted."
responseprobs.abortion.combined$whichresponse[responseprobs.abortion.combined$whichresponse=="2"] <- "The law should permit abortion only in cases of rape,\nincest, or when the woman's life is in danger."
responseprobs.abortion.combined$whichresponse[responseprobs.abortion.combined$whichresponse=="3"] <- "The law should permit abortion for reasons other than\nrape, incest, or danger to the woman's life, but only after\n the need for abortion has been clearly established."
responseprobs.abortion.combined$whichresponse[responseprobs.abortion.combined$whichresponse=="4"] <- "By law, a woman should always be able to obtain an\nabortion as a matter of personal choice."
#
responseprobs.abortion.plotdf <- melt(responseprobs.abortion.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.abortion.plotdf$whichresponse <- fct_inorder(responseprobs.abortion.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_abortion.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.abortion.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Abortion",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.libcon.Pi1 <- data.frame(issue="libcon", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,11],
  ideal2=responseprobs.Z[,2,1,11],
  ideal3=responseprobs.Z[,3,1,11],
  ideal4=responseprobs.Z[,4,1,11],
  ideal5=responseprobs.Z[,5,1,11])
#
responseprobs.libcon.Pi2 <- data.frame(issue="libcon", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,11] - responseprobs.Z[,1,1,11],
  ideal2=responseprobs.Z[,2,2,11] - responseprobs.Z[,2,1,11],
  ideal3=responseprobs.Z[,3,2,11] - responseprobs.Z[,3,1,11],
  ideal4=responseprobs.Z[,4,2,11] - responseprobs.Z[,4,1,11],
  ideal5=responseprobs.Z[,5,2,11] - responseprobs.Z[,5,1,11])
#
responseprobs.libcon.Pi3 <- data.frame(issue="libcon", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,11] - responseprobs.Z[,1,2,11],
  ideal2=responseprobs.Z[,2,3,11] - responseprobs.Z[,2,2,11],
  ideal3=responseprobs.Z[,3,3,11] - responseprobs.Z[,3,2,11],
  ideal4=responseprobs.Z[,4,3,11] - responseprobs.Z[,4,2,11],
  ideal5=responseprobs.Z[,5,3,11] - responseprobs.Z[,5,2,11])
#
responseprobs.libcon.Pi4 <- data.frame(issue="libcon", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,4,11] - responseprobs.Z[,1,3,11],
  ideal2=responseprobs.Z[,2,4,11] - responseprobs.Z[,2,3,11],
  ideal3=responseprobs.Z[,3,4,11] - responseprobs.Z[,3,3,11],
  ideal4=responseprobs.Z[,4,4,11] - responseprobs.Z[,4,3,11],
  ideal5=responseprobs.Z[,5,4,11] - responseprobs.Z[,5,3,11])
#
responseprobs.libcon.Pi5 <- data.frame(issue="libcon", whichresponse="5", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,5,11] - responseprobs.Z[,1,4,11],
  ideal2=responseprobs.Z[,2,5,11] - responseprobs.Z[,2,4,11],
  ideal3=responseprobs.Z[,3,5,11] - responseprobs.Z[,3,4,11],
  ideal4=responseprobs.Z[,4,5,11] - responseprobs.Z[,4,4,11],
  ideal5=responseprobs.Z[,5,5,11] - responseprobs.Z[,5,4,11])
#
responseprobs.libcon.Pi6 <- data.frame(issue="libcon", whichresponse="6", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,6,11] - responseprobs.Z[,1,5,11],
  ideal2=responseprobs.Z[,2,6,11] - responseprobs.Z[,2,5,11],
  ideal3=responseprobs.Z[,3,6,11] - responseprobs.Z[,3,5,11],
  ideal4=responseprobs.Z[,4,6,11] - responseprobs.Z[,4,5,11],
  ideal5=responseprobs.Z[,5,6,11] - responseprobs.Z[,5,5,11])
#
responseprobs.libcon.Pi7 <- data.frame(issue="libcon", whichresponse="7", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,6,11],
  ideal2=1 - responseprobs.Z[,2,6,11],
  ideal3=1 - responseprobs.Z[,3,6,11],
  ideal4=1 - responseprobs.Z[,4,6,11],
  ideal5=1 - responseprobs.Z[,5,6,11])
#
responseprobs.libcon.combined <- rbind(responseprobs.libcon.Pi1, responseprobs.libcon.Pi2, responseprobs.libcon.Pi3, responseprobs.libcon.Pi4, responseprobs.libcon.Pi5, responseprobs.libcon.Pi6, responseprobs.libcon.Pi7)
#
names(responseprobs.libcon.combined)[which(names(responseprobs.libcon.combined)=="ideal1")] <- "10%"
names(responseprobs.libcon.combined)[which(names(responseprobs.libcon.combined)=="ideal2")] <- "25%"
names(responseprobs.libcon.combined)[which(names(responseprobs.libcon.combined)=="ideal3")] <- "50%"
names(responseprobs.libcon.combined)[which(names(responseprobs.libcon.combined)=="ideal4")] <- "75%"
names(responseprobs.libcon.combined)[which(names(responseprobs.libcon.combined)=="ideal5")] <- "90%"
#
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="1"] <- "Extremely liberal"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="2"] <- "Liberal"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="3"] <- "Slightly liberal"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="4"] <- "Moderate, middle of the road"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="5"] <- "Slightly conservative"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="6"] <- "Conservative"
responseprobs.libcon.combined$whichresponse[responseprobs.libcon.combined$whichresponse=="7"] <- "Extremely conservative"
#
responseprobs.libcon.plotdf <- melt(responseprobs.libcon.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.libcon.plotdf$whichresponse <- fct_inorder(responseprobs.libcon.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_libcon.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.libcon.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Liberal-conservative self-identification",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.govtservices.Pi1 <- data.frame(issue="govtservices", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,12],
  ideal2=responseprobs.Z[,2,1,12],
  ideal3=responseprobs.Z[,3,1,12],
  ideal4=responseprobs.Z[,4,1,12],
  ideal5=responseprobs.Z[,5,1,12])
#
responseprobs.govtservices.Pi2 <- data.frame(issue="govtservices", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,12] - responseprobs.Z[,1,1,12],
  ideal2=responseprobs.Z[,2,2,12] - responseprobs.Z[,2,1,12],
  ideal3=responseprobs.Z[,3,2,12] - responseprobs.Z[,3,1,12],
  ideal4=responseprobs.Z[,4,2,12] - responseprobs.Z[,4,1,12],
  ideal5=responseprobs.Z[,5,2,12] - responseprobs.Z[,5,1,12])
#
responseprobs.govtservices.Pi3 <- data.frame(issue="govtservices", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,12] - responseprobs.Z[,1,2,12],
  ideal2=responseprobs.Z[,2,3,12] - responseprobs.Z[,2,2,12],
  ideal3=responseprobs.Z[,3,3,12] - responseprobs.Z[,3,2,12],
  ideal4=responseprobs.Z[,4,3,12] - responseprobs.Z[,4,2,12],
  ideal5=responseprobs.Z[,5,3,12] - responseprobs.Z[,5,2,12])
#
responseprobs.govtservices.Pi4 <- data.frame(issue="govtservices", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,4,12] - responseprobs.Z[,1,3,12],
  ideal2=responseprobs.Z[,2,4,12] - responseprobs.Z[,2,3,12],
  ideal3=responseprobs.Z[,3,4,12] - responseprobs.Z[,3,3,12],
  ideal4=responseprobs.Z[,4,4,12] - responseprobs.Z[,4,3,12],
  ideal5=responseprobs.Z[,5,4,12] - responseprobs.Z[,5,3,12])
#
responseprobs.govtservices.Pi5 <- data.frame(issue="govtservices", whichresponse="5", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,5,12] - responseprobs.Z[,1,4,12],
  ideal2=responseprobs.Z[,2,5,12] - responseprobs.Z[,2,4,12],
  ideal3=responseprobs.Z[,3,5,12] - responseprobs.Z[,3,4,12],
  ideal4=responseprobs.Z[,4,5,12] - responseprobs.Z[,4,4,12],
  ideal5=responseprobs.Z[,5,5,12] - responseprobs.Z[,5,4,12])
#
responseprobs.govtservices.Pi6 <- data.frame(issue="govtservices", whichresponse="6", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,6,12] - responseprobs.Z[,1,5,12],
  ideal2=responseprobs.Z[,2,6,12] - responseprobs.Z[,2,5,12],
  ideal3=responseprobs.Z[,3,6,12] - responseprobs.Z[,3,5,12],
  ideal4=responseprobs.Z[,4,6,12] - responseprobs.Z[,4,5,12],
  ideal5=responseprobs.Z[,5,6,12] - responseprobs.Z[,5,5,12])
#
responseprobs.govtservices.Pi7 <- data.frame(issue="govtservices", whichresponse="7", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,6,12],
  ideal2=1 - responseprobs.Z[,2,6,12],
  ideal3=1 - responseprobs.Z[,3,6,12],
  ideal4=1 - responseprobs.Z[,4,6,12],
  ideal5=1 - responseprobs.Z[,5,6,12])
#
responseprobs.govtservices.combined <- rbind(responseprobs.govtservices.Pi1, responseprobs.govtservices.Pi2, responseprobs.govtservices.Pi3, responseprobs.govtservices.Pi4, responseprobs.govtservices.Pi5, responseprobs.govtservices.Pi6, responseprobs.govtservices.Pi7)
#
names(responseprobs.govtservices.combined)[which(names(responseprobs.govtservices.combined)=="ideal1")] <- "10%"
names(responseprobs.govtservices.combined)[which(names(responseprobs.govtservices.combined)=="ideal2")] <- "25%"
names(responseprobs.govtservices.combined)[which(names(responseprobs.govtservices.combined)=="ideal3")] <- "50%"
names(responseprobs.govtservices.combined)[which(names(responseprobs.govtservices.combined)=="ideal4")] <- "75%"
names(responseprobs.govtservices.combined)[which(names(responseprobs.govtservices.combined)=="ideal5")] <- "90%"
#
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="1"] <- "1: Cut govt services\nand spending"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="2"] <- "2"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="3"] <- "3"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="4"] <- "4"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="5"] <- "5"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="6"] <- "6"
responseprobs.govtservices.combined$whichresponse[responseprobs.govtservices.combined$whichresponse=="7"] <- "7: More govt services\nand spending"
#
responseprobs.govtservices.plotdf <- melt(responseprobs.govtservices.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.govtservices.plotdf$whichresponse <- fct_inorder(responseprobs.govtservices.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_govtservices.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.govtservices.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Government spending and services",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.guarjobs.Pi1 <- data.frame(issue="guarjobs", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,13],
  ideal2=responseprobs.Z[,2,1,13],
  ideal3=responseprobs.Z[,3,1,13],
  ideal4=responseprobs.Z[,4,1,13],
  ideal5=responseprobs.Z[,5,1,13])
#
responseprobs.guarjobs.Pi2 <- data.frame(issue="guarjobs", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,13] - responseprobs.Z[,1,1,13],
  ideal2=responseprobs.Z[,2,2,13] - responseprobs.Z[,2,1,13],
  ideal3=responseprobs.Z[,3,2,13] - responseprobs.Z[,3,1,13],
  ideal4=responseprobs.Z[,4,2,13] - responseprobs.Z[,4,1,13],
  ideal5=responseprobs.Z[,5,2,13] - responseprobs.Z[,5,1,13])
#
responseprobs.guarjobs.Pi3 <- data.frame(issue="guarjobs", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,13] - responseprobs.Z[,1,2,13],
  ideal2=responseprobs.Z[,2,3,13] - responseprobs.Z[,2,2,13],
  ideal3=responseprobs.Z[,3,3,13] - responseprobs.Z[,3,2,13],
  ideal4=responseprobs.Z[,4,3,13] - responseprobs.Z[,4,2,13],
  ideal5=responseprobs.Z[,5,3,13] - responseprobs.Z[,5,2,13])
#
responseprobs.guarjobs.Pi4 <- data.frame(issue="guarjobs", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,4,13] - responseprobs.Z[,1,3,13],
  ideal2=responseprobs.Z[,2,4,13] - responseprobs.Z[,2,3,13],
  ideal3=responseprobs.Z[,3,4,13] - responseprobs.Z[,3,3,13],
  ideal4=responseprobs.Z[,4,4,13] - responseprobs.Z[,4,3,13],
  ideal5=responseprobs.Z[,5,4,13] - responseprobs.Z[,5,3,13])
#
responseprobs.guarjobs.Pi5 <- data.frame(issue="guarjobs", whichresponse="5", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,5,13] - responseprobs.Z[,1,4,13],
  ideal2=responseprobs.Z[,2,5,13] - responseprobs.Z[,2,4,13],
  ideal3=responseprobs.Z[,3,5,13] - responseprobs.Z[,3,4,13],
  ideal4=responseprobs.Z[,4,5,13] - responseprobs.Z[,4,4,13],
  ideal5=responseprobs.Z[,5,5,13] - responseprobs.Z[,5,4,13])
#
responseprobs.guarjobs.Pi6 <- data.frame(issue="guarjobs", whichresponse="6", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,6,13] - responseprobs.Z[,1,5,13],
  ideal2=responseprobs.Z[,2,6,13] - responseprobs.Z[,2,5,13],
  ideal3=responseprobs.Z[,3,6,13] - responseprobs.Z[,3,5,13],
  ideal4=responseprobs.Z[,4,6,13] - responseprobs.Z[,4,5,13],
  ideal5=responseprobs.Z[,5,6,13] - responseprobs.Z[,5,5,13])
#
responseprobs.guarjobs.Pi7 <- data.frame(issue="guarjobs", whichresponse="7", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,6,13],
  ideal2=1 - responseprobs.Z[,2,6,13],
  ideal3=1 - responseprobs.Z[,3,6,13],
  ideal4=1 - responseprobs.Z[,4,6,13],
  ideal5=1 - responseprobs.Z[,5,6,13])
#
responseprobs.guarjobs.combined <- rbind(responseprobs.guarjobs.Pi1, responseprobs.guarjobs.Pi2, responseprobs.guarjobs.Pi3, responseprobs.guarjobs.Pi4, responseprobs.guarjobs.Pi5, responseprobs.guarjobs.Pi6, responseprobs.guarjobs.Pi7)
#
names(responseprobs.guarjobs.combined)[which(names(responseprobs.guarjobs.combined)=="ideal1")] <- "10%"
names(responseprobs.guarjobs.combined)[which(names(responseprobs.guarjobs.combined)=="ideal2")] <- "25%"
names(responseprobs.guarjobs.combined)[which(names(responseprobs.guarjobs.combined)=="ideal3")] <- "50%"
names(responseprobs.guarjobs.combined)[which(names(responseprobs.guarjobs.combined)=="ideal4")] <- "75%"
names(responseprobs.guarjobs.combined)[which(names(responseprobs.guarjobs.combined)=="ideal5")] <- "90%"
#
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="1"] <- "1: Govt see to job and\nstandard of living"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="2"] <- "2"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="3"] <- "3"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="4"] <- "4"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="5"] <- "5"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="6"] <- "6"
responseprobs.guarjobs.combined$whichresponse[responseprobs.guarjobs.combined$whichresponse=="7"] <- "7: Each person on own"
#
responseprobs.guarjobs.plotdf <- melt(responseprobs.guarjobs.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.guarjobs.plotdf$whichresponse <- fct_inorder(responseprobs.guarjobs.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_guarjobs.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.guarjobs.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Guaranteed jobs and standard of living",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.aidtoblacks.Pi1 <- data.frame(issue="aidtoblacks", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,16],
  ideal2=responseprobs.Z[,2,1,16],
  ideal3=responseprobs.Z[,3,1,16],
  ideal4=responseprobs.Z[,4,1,16],
  ideal5=responseprobs.Z[,5,1,16])
#
responseprobs.aidtoblacks.Pi2 <- data.frame(issue="aidtoblacks", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,16] - responseprobs.Z[,1,1,16],
  ideal2=responseprobs.Z[,2,2,16] - responseprobs.Z[,2,1,16],
  ideal3=responseprobs.Z[,3,2,16] - responseprobs.Z[,3,1,16],
  ideal4=responseprobs.Z[,4,2,16] - responseprobs.Z[,4,1,16],
  ideal5=responseprobs.Z[,5,2,16] - responseprobs.Z[,5,1,16])
#
responseprobs.aidtoblacks.Pi3 <- data.frame(issue="aidtoblacks", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,16] - responseprobs.Z[,1,2,16],
  ideal2=responseprobs.Z[,2,3,16] - responseprobs.Z[,2,2,16],
  ideal3=responseprobs.Z[,3,3,16] - responseprobs.Z[,3,2,16],
  ideal4=responseprobs.Z[,4,3,16] - responseprobs.Z[,4,2,16],
  ideal5=responseprobs.Z[,5,3,16] - responseprobs.Z[,5,2,16])
#
responseprobs.aidtoblacks.Pi4 <- data.frame(issue="aidtoblacks", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,4,16] - responseprobs.Z[,1,3,16],
  ideal2=responseprobs.Z[,2,4,16] - responseprobs.Z[,2,3,16],
  ideal3=responseprobs.Z[,3,4,16] - responseprobs.Z[,3,3,16],
  ideal4=responseprobs.Z[,4,4,16] - responseprobs.Z[,4,3,16],
  ideal5=responseprobs.Z[,5,4,16] - responseprobs.Z[,5,3,16])
#
responseprobs.aidtoblacks.Pi5 <- data.frame(issue="aidtoblacks", whichresponse="5", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,5,16] - responseprobs.Z[,1,4,16],
  ideal2=responseprobs.Z[,2,5,16] - responseprobs.Z[,2,4,16],
  ideal3=responseprobs.Z[,3,5,16] - responseprobs.Z[,3,4,16],
  ideal4=responseprobs.Z[,4,5,16] - responseprobs.Z[,4,4,16],
  ideal5=responseprobs.Z[,5,5,16] - responseprobs.Z[,5,4,16])
#
responseprobs.aidtoblacks.Pi6 <- data.frame(issue="aidtoblacks", whichresponse="6", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,6,16] - responseprobs.Z[,1,5,16],
  ideal2=responseprobs.Z[,2,6,16] - responseprobs.Z[,2,5,16],
  ideal3=responseprobs.Z[,3,6,16] - responseprobs.Z[,3,5,16],
  ideal4=responseprobs.Z[,4,6,16] - responseprobs.Z[,4,5,16],
  ideal5=responseprobs.Z[,5,6,16] - responseprobs.Z[,5,5,16])
#
responseprobs.aidtoblacks.Pi7 <- data.frame(issue="aidtoblacks", whichresponse="7", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,6,16],
  ideal2=1 - responseprobs.Z[,2,6,16],
  ideal3=1 - responseprobs.Z[,3,6,16],
  ideal4=1 - responseprobs.Z[,4,6,16],
  ideal5=1 - responseprobs.Z[,5,6,16])
#
responseprobs.aidtoblacks.combined <- rbind(responseprobs.aidtoblacks.Pi1, responseprobs.aidtoblacks.Pi2, responseprobs.aidtoblacks.Pi3, responseprobs.aidtoblacks.Pi4, responseprobs.aidtoblacks.Pi5, responseprobs.aidtoblacks.Pi6, responseprobs.aidtoblacks.Pi7)
#
names(responseprobs.aidtoblacks.combined)[which(names(responseprobs.aidtoblacks.combined)=="ideal1")] <- "10%"
names(responseprobs.aidtoblacks.combined)[which(names(responseprobs.aidtoblacks.combined)=="ideal2")] <- "25%"
names(responseprobs.aidtoblacks.combined)[which(names(responseprobs.aidtoblacks.combined)=="ideal3")] <- "50%"
names(responseprobs.aidtoblacks.combined)[which(names(responseprobs.aidtoblacks.combined)=="ideal4")] <- "75%"
names(responseprobs.aidtoblacks.combined)[which(names(responseprobs.aidtoblacks.combined)=="ideal5")] <- "90%"
#
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="1"] <- "1: Govt should help\nblacks/minority groups"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="2"] <- "2"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="3"] <- "3"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="4"] <- "4"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="5"] <- "5"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="6"] <- "6"
responseprobs.aidtoblacks.combined$whichresponse[responseprobs.aidtoblacks.combined$whichresponse=="7"] <- "7: Blacks/minority groups\nshould help selves"
#
responseprobs.aidtoblacks.plotdf <- melt(responseprobs.aidtoblacks.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.aidtoblacks.plotdf$whichresponse <- fct_inorder(responseprobs.aidtoblacks.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_aidtoblacks.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.aidtoblacks.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Aid to blacks and minorities",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.defensespending.Pi1 <- data.frame(issue="defensespending", whichresponse="1", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,1,17],
  ideal2=responseprobs.Z[,2,1,17],
  ideal3=responseprobs.Z[,3,1,17],
  ideal4=responseprobs.Z[,4,1,17],
  ideal5=responseprobs.Z[,5,1,17])
#
responseprobs.defensespending.Pi2 <- data.frame(issue="defensespending", whichresponse="2", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,2,17] - responseprobs.Z[,1,1,17],
  ideal2=responseprobs.Z[,2,2,17] - responseprobs.Z[,2,1,17],
  ideal3=responseprobs.Z[,3,2,17] - responseprobs.Z[,3,1,17],
  ideal4=responseprobs.Z[,4,2,17] - responseprobs.Z[,4,1,17],
  ideal5=responseprobs.Z[,5,2,17] - responseprobs.Z[,5,1,17])
#
responseprobs.defensespending.Pi3 <- data.frame(issue="defensespending", whichresponse="3", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,3,17] - responseprobs.Z[,1,2,17],
  ideal2=responseprobs.Z[,2,3,17] - responseprobs.Z[,2,2,17],
  ideal3=responseprobs.Z[,3,3,17] - responseprobs.Z[,3,2,17],
  ideal4=responseprobs.Z[,4,3,17] - responseprobs.Z[,4,2,17],
  ideal5=responseprobs.Z[,5,3,17] - responseprobs.Z[,5,2,17])
#
responseprobs.defensespending.Pi4 <- data.frame(issue="defensespending", whichresponse="4", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,4,17] - responseprobs.Z[,1,3,17],
  ideal2=responseprobs.Z[,2,4,17] - responseprobs.Z[,2,3,17],
  ideal3=responseprobs.Z[,3,4,17] - responseprobs.Z[,3,3,17],
  ideal4=responseprobs.Z[,4,4,17] - responseprobs.Z[,4,3,17],
  ideal5=responseprobs.Z[,5,4,17] - responseprobs.Z[,5,3,17])
#
responseprobs.defensespending.Pi5 <- data.frame(issue="defensespending", whichresponse="5", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,5,17] - responseprobs.Z[,1,4,17],
  ideal2=responseprobs.Z[,2,5,17] - responseprobs.Z[,2,4,17],
  ideal3=responseprobs.Z[,3,5,17] - responseprobs.Z[,3,4,17],
  ideal4=responseprobs.Z[,4,5,17] - responseprobs.Z[,4,4,17],
  ideal5=responseprobs.Z[,5,5,17] - responseprobs.Z[,5,4,17])
#
responseprobs.defensespending.Pi6 <- data.frame(issue="defensespending", whichresponse="6", time=seq(1980,2016,by=4),
  ideal1=responseprobs.Z[,1,6,17] - responseprobs.Z[,1,5,17],
  ideal2=responseprobs.Z[,2,6,17] - responseprobs.Z[,2,5,17],
  ideal3=responseprobs.Z[,3,6,17] - responseprobs.Z[,3,5,17],
  ideal4=responseprobs.Z[,4,6,17] - responseprobs.Z[,4,5,17],
  ideal5=responseprobs.Z[,5,6,17] - responseprobs.Z[,5,5,17])
#
responseprobs.defensespending.Pi7 <- data.frame(issue="defensespending", whichresponse="7", time=seq(1980,2016,by=4),
  ideal1=1 - responseprobs.Z[,1,6,17],
  ideal2=1 - responseprobs.Z[,2,6,17],
  ideal3=1 - responseprobs.Z[,3,6,17],
  ideal4=1 - responseprobs.Z[,4,6,17],
  ideal5=1 - responseprobs.Z[,5,6,17])
#
responseprobs.defensespending.combined <- rbind(responseprobs.defensespending.Pi1, responseprobs.defensespending.Pi2, responseprobs.defensespending.Pi3, responseprobs.defensespending.Pi4, responseprobs.defensespending.Pi5, responseprobs.defensespending.Pi6, responseprobs.defensespending.Pi7)
#
names(responseprobs.defensespending.combined)[which(names(responseprobs.defensespending.combined)=="ideal1")] <- "10%"
names(responseprobs.defensespending.combined)[which(names(responseprobs.defensespending.combined)=="ideal2")] <- "25%"
names(responseprobs.defensespending.combined)[which(names(responseprobs.defensespending.combined)=="ideal3")] <- "50%"
names(responseprobs.defensespending.combined)[which(names(responseprobs.defensespending.combined)=="ideal4")] <- "75%"
names(responseprobs.defensespending.combined)[which(names(responseprobs.defensespending.combined)=="ideal5")] <- "90%"
#
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="1"] <- "1: Greatly decrease\ndefense spending"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="2"] <- "2"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="3"] <- "3"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="4"] <- "4"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="5"] <- "5"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="6"] <- "6"
responseprobs.defensespending.combined$whichresponse[responseprobs.defensespending.combined$whichresponse=="7"] <- "7: Greatly increase\ndefense spending"
#
responseprobs.defensespending.plotdf <- melt(responseprobs.defensespending.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.defensespending.plotdf$whichresponse <- fct_inorder(responseprobs.defensespending.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_defensespending.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.defensespending.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="Defense spending",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.environmentjobs.Pi1 <- data.frame(issue="environmentjobs", whichresponse="1", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,1,15],
  ideal2=responseprobs.Z[5:10,2,1,15],
  ideal3=responseprobs.Z[5:10,3,1,15],
  ideal4=responseprobs.Z[5:10,4,1,15],
  ideal5=responseprobs.Z[5:10,5,1,15])
#
responseprobs.environmentjobs.Pi2 <- data.frame(issue="environmentjobs", whichresponse="2", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,2,15] - responseprobs.Z[5:10,1,1,15],
  ideal2=responseprobs.Z[5:10,2,2,15] - responseprobs.Z[5:10,2,1,15],
  ideal3=responseprobs.Z[5:10,3,2,15] - responseprobs.Z[5:10,3,1,15],
  ideal4=responseprobs.Z[5:10,4,2,15] - responseprobs.Z[5:10,4,1,15],
  ideal5=responseprobs.Z[5:10,5,2,15] - responseprobs.Z[5:10,5,1,15])
#
responseprobs.environmentjobs.Pi3 <- data.frame(issue="environmentjobs", whichresponse="3", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,3,15] - responseprobs.Z[5:10,1,2,15],
  ideal2=responseprobs.Z[5:10,2,3,15] - responseprobs.Z[5:10,2,2,15],
  ideal3=responseprobs.Z[5:10,3,3,15] - responseprobs.Z[5:10,3,2,15],
  ideal4=responseprobs.Z[5:10,4,3,15] - responseprobs.Z[5:10,4,2,15],
  ideal5=responseprobs.Z[5:10,5,3,15] - responseprobs.Z[5:10,5,2,15])
#
responseprobs.environmentjobs.Pi4 <- data.frame(issue="environmentjobs", whichresponse="4", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,4,15] - responseprobs.Z[5:10,1,3,15],
  ideal2=responseprobs.Z[5:10,2,4,15] - responseprobs.Z[5:10,2,3,15],
  ideal3=responseprobs.Z[5:10,3,4,15] - responseprobs.Z[5:10,3,3,15],
  ideal4=responseprobs.Z[5:10,4,4,15] - responseprobs.Z[5:10,4,3,15],
  ideal5=responseprobs.Z[5:10,5,4,15] - responseprobs.Z[5:10,5,3,15])
#
responseprobs.environmentjobs.Pi5 <- data.frame(issue="environmentjobs", whichresponse="5", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,5,15] - responseprobs.Z[5:10,1,4,15],
  ideal2=responseprobs.Z[5:10,2,5,15] - responseprobs.Z[5:10,2,4,15],
  ideal3=responseprobs.Z[5:10,3,5,15] - responseprobs.Z[5:10,3,4,15],
  ideal4=responseprobs.Z[5:10,4,5,15] - responseprobs.Z[5:10,4,4,15],
  ideal5=responseprobs.Z[5:10,5,5,15] - responseprobs.Z[5:10,5,4,15])
#
responseprobs.environmentjobs.Pi6 <- data.frame(issue="environmentjobs", whichresponse="6", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,6,15] - responseprobs.Z[5:10,1,5,15],
  ideal2=responseprobs.Z[5:10,2,6,15] - responseprobs.Z[5:10,2,5,15],
  ideal3=responseprobs.Z[5:10,3,6,15] - responseprobs.Z[5:10,3,5,15],
  ideal4=responseprobs.Z[5:10,4,6,15] - responseprobs.Z[5:10,4,5,15],
  ideal5=responseprobs.Z[5:10,5,6,15] - responseprobs.Z[5:10,5,5,15])
#
responseprobs.environmentjobs.Pi7 <- data.frame(issue="environmentjobs", whichresponse="7", time=seq(1996,2016,by=4),
  ideal1=1 - responseprobs.Z[5:10,1,6,15],
  ideal2=1 - responseprobs.Z[5:10,2,6,15],
  ideal3=1 - responseprobs.Z[5:10,3,6,15],
  ideal4=1 - responseprobs.Z[5:10,4,6,15],
  ideal5=1 - responseprobs.Z[5:10,5,6,15])
#
responseprobs.environmentjobs.combined <- rbind(responseprobs.environmentjobs.Pi1, responseprobs.environmentjobs.Pi2, responseprobs.environmentjobs.Pi3, responseprobs.environmentjobs.Pi4, responseprobs.environmentjobs.Pi5, responseprobs.environmentjobs.Pi6, responseprobs.environmentjobs.Pi7)
#
names(responseprobs.environmentjobs.combined)[which(names(responseprobs.environmentjobs.combined)=="ideal1")] <- "10%"
names(responseprobs.environmentjobs.combined)[which(names(responseprobs.environmentjobs.combined)=="ideal2")] <- "25%"
names(responseprobs.environmentjobs.combined)[which(names(responseprobs.environmentjobs.combined)=="ideal3")] <- "50%"
names(responseprobs.environmentjobs.combined)[which(names(responseprobs.environmentjobs.combined)=="ideal4")] <- "75%"
names(responseprobs.environmentjobs.combined)[which(names(responseprobs.environmentjobs.combined)=="ideal5")] <- "90%"
#
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="1"] <- "1: Protect environment"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="2"] <- "2"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="3"] <- "3"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="4"] <- "4"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="5"] <- "5"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="6"] <- "6"
responseprobs.environmentjobs.combined$whichresponse[responseprobs.environmentjobs.combined$whichresponse=="7"] <- "7: Protect jobs"
#
responseprobs.environmentjobs.plotdf <- melt(responseprobs.environmentjobs.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.environmentjobs.plotdf$whichresponse <- fct_inorder(responseprobs.environmentjobs.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_environmentjobs.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.environmentjobs.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1996-2016\n") +
  labs(subtitle="Environment-jobs tradeoff",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.gaydiscrimination.Pi1 <- data.frame(issue="gaydiscrimination", whichresponse="1", time=seq(1988,2016,by=4),
  ideal1=responseprobs.Z[3:10,1,1,2],
  ideal2=responseprobs.Z[3:10,2,1,2],
  ideal3=responseprobs.Z[3:10,3,1,2],
  ideal4=responseprobs.Z[3:10,4,1,2],
  ideal5=responseprobs.Z[3:10,5,1,2])
#
responseprobs.gaydiscrimination.Pi2 <- data.frame(issue="gaydiscrimination", whichresponse="2", time=seq(1988,2016,by=4),
  ideal1=responseprobs.Z[3:10,1,2,2] - responseprobs.Z[3:10,1,1,2],
  ideal2=responseprobs.Z[3:10,2,2,2] - responseprobs.Z[3:10,2,1,2],
  ideal3=responseprobs.Z[3:10,3,2,2] - responseprobs.Z[3:10,3,1,2],
  ideal4=responseprobs.Z[3:10,4,2,2] - responseprobs.Z[3:10,4,1,2],
  ideal5=responseprobs.Z[3:10,5,2,2] - responseprobs.Z[3:10,5,1,2])
#
responseprobs.gaydiscrimination.Pi3 <- data.frame(issue="gaydiscrimination", whichresponse="3", time=seq(1988,2016,by=4),
  ideal1=responseprobs.Z[3:10,1,3,2] - responseprobs.Z[3:10,1,2,2],
  ideal2=responseprobs.Z[3:10,2,3,2] - responseprobs.Z[3:10,2,2,2],
  ideal3=responseprobs.Z[3:10,3,3,2] - responseprobs.Z[3:10,3,2,2],
  ideal4=responseprobs.Z[3:10,4,3,2] - responseprobs.Z[3:10,4,2,2],
  ideal5=responseprobs.Z[3:10,5,3,2] - responseprobs.Z[3:10,5,2,2])
#
responseprobs.gaydiscrimination.Pi4 <- data.frame(issue="gaydiscrimination", whichresponse="4", time=seq(1988,2016,by=4),
  ideal1=1 - responseprobs.Z[3:10,1,3,2],
  ideal2=1 - responseprobs.Z[3:10,2,3,2],
  ideal3=1 - responseprobs.Z[3:10,3,3,2],
  ideal4=1 - responseprobs.Z[3:10,4,3,2],
  ideal5=1 - responseprobs.Z[3:10,5,3,2])
#
responseprobs.gaydiscrimination.combined <- rbind(responseprobs.gaydiscrimination.Pi1, responseprobs.gaydiscrimination.Pi2, responseprobs.gaydiscrimination.Pi3, responseprobs.gaydiscrimination.Pi4)
#
names(responseprobs.gaydiscrimination.combined)[which(names(responseprobs.gaydiscrimination.combined)=="ideal1")] <- "10%"
names(responseprobs.gaydiscrimination.combined)[which(names(responseprobs.gaydiscrimination.combined)=="ideal2")] <- "25%"
names(responseprobs.gaydiscrimination.combined)[which(names(responseprobs.gaydiscrimination.combined)=="ideal3")] <- "50%"
names(responseprobs.gaydiscrimination.combined)[which(names(responseprobs.gaydiscrimination.combined)=="ideal4")] <- "75%"
names(responseprobs.gaydiscrimination.combined)[which(names(responseprobs.gaydiscrimination.combined)=="ideal5")] <- "90%"
#
responseprobs.gaydiscrimination.combined$whichresponse[responseprobs.gaydiscrimination.combined$whichresponse=="1"] <- "Favor strongly"
responseprobs.gaydiscrimination.combined$whichresponse[responseprobs.gaydiscrimination.combined$whichresponse=="2"] <- "Favor not strongly"
responseprobs.gaydiscrimination.combined$whichresponse[responseprobs.gaydiscrimination.combined$whichresponse=="3"] <- "Oppose not strongly"
responseprobs.gaydiscrimination.combined$whichresponse[responseprobs.gaydiscrimination.combined$whichresponse=="4"] <- "Oppose strongly"
#
responseprobs.gaydiscrimination.plotdf <- melt(responseprobs.gaydiscrimination.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.gaydiscrimination.plotdf$whichresponse <- fct_inorder(responseprobs.gaydiscrimination.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_gaydiscrimination.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.gaydiscrimination.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1988-2016\n") +
  labs(subtitle="Laws to protect gays and lesbians against job discrimination",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.guncontrol.Pi1 <- data.frame(issue="guncontrol", whichresponse="1", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,1,7],
  ideal2=responseprobs.Z[5:10,2,1,7],
  ideal3=responseprobs.Z[5:10,3,1,7],
  ideal4=responseprobs.Z[5:10,4,1,7],
  ideal5=responseprobs.Z[5:10,5,1,7])
#
responseprobs.guncontrol.Pi2 <- data.frame(issue="guncontrol", whichresponse="2", time=seq(1996,2016,by=4),
  ideal1=responseprobs.Z[5:10,1,2,7] - responseprobs.Z[5:10,1,1,7],
  ideal2=responseprobs.Z[5:10,2,2,7] - responseprobs.Z[5:10,2,1,7],
  ideal3=responseprobs.Z[5:10,3,2,7] - responseprobs.Z[5:10,3,1,7],
  ideal4=responseprobs.Z[5:10,4,2,7] - responseprobs.Z[5:10,4,1,7],
  ideal5=responseprobs.Z[5:10,5,2,7] - responseprobs.Z[5:10,5,1,7])
#
responseprobs.guncontrol.Pi3 <- data.frame(issue="guncontrol", whichresponse="3", time=seq(1996,2016,by=4),
  ideal1=1 - responseprobs.Z[5:10,1,2,7],
  ideal2=1 - responseprobs.Z[5:10,2,2,7],
  ideal3=1 - responseprobs.Z[5:10,3,2,7],
  ideal4=1 - responseprobs.Z[5:10,4,2,7],
  ideal5=1 - responseprobs.Z[5:10,5,2,7])
#
responseprobs.guncontrol.combined <- rbind(responseprobs.guncontrol.Pi1, responseprobs.guncontrol.Pi2, responseprobs.guncontrol.Pi3)
#
names(responseprobs.guncontrol.combined)[which(names(responseprobs.guncontrol.combined)=="ideal1")] <- "10%"
names(responseprobs.guncontrol.combined)[which(names(responseprobs.guncontrol.combined)=="ideal2")] <- "25%"
names(responseprobs.guncontrol.combined)[which(names(responseprobs.guncontrol.combined)=="ideal3")] <- "50%"
names(responseprobs.guncontrol.combined)[which(names(responseprobs.guncontrol.combined)=="ideal4")] <- "75%"
names(responseprobs.guncontrol.combined)[which(names(responseprobs.guncontrol.combined)=="ideal5")] <- "90%"
#
responseprobs.guncontrol.combined$whichresponse[responseprobs.guncontrol.combined$whichresponse=="1"] <- "More difficult"
responseprobs.guncontrol.combined$whichresponse[responseprobs.guncontrol.combined$whichresponse=="2"] <- "Keep rules about\nthe same"
responseprobs.guncontrol.combined$whichresponse[responseprobs.guncontrol.combined$whichresponse=="3"] <- "Easier"
#
responseprobs.guncontrol.plotdf <- melt(responseprobs.guncontrol.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.guncontrol.plotdf$whichresponse <- fct_inorder(responseprobs.guncontrol.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_guncontrol.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.guncontrol.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1996-2016\n") +
  labs(subtitle="Rules to purchase guns",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.healthinsurance.Pi1 <- data.frame(issue="healthinsurance", whichresponse="1", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,1,14],
  ideal2=responseprobs.Z[2:10,2,1,14],
  ideal3=responseprobs.Z[2:10,3,1,14],
  ideal4=responseprobs.Z[2:10,4,1,14],
  ideal5=responseprobs.Z[2:10,5,1,14])
#
responseprobs.healthinsurance.Pi2 <- data.frame(issue="healthinsurance", whichresponse="2", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,2,14] - responseprobs.Z[2:10,1,1,14],
  ideal2=responseprobs.Z[2:10,2,2,14] - responseprobs.Z[2:10,2,1,14],
  ideal3=responseprobs.Z[2:10,3,2,14] - responseprobs.Z[2:10,3,1,14],
  ideal4=responseprobs.Z[2:10,4,2,14] - responseprobs.Z[2:10,4,1,14],
  ideal5=responseprobs.Z[2:10,5,2,14] - responseprobs.Z[2:10,5,1,14])
#
responseprobs.healthinsurance.Pi3 <- data.frame(issue="healthinsurance", whichresponse="3", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,3,14] - responseprobs.Z[2:10,1,2,14],
  ideal2=responseprobs.Z[2:10,2,3,14] - responseprobs.Z[2:10,2,2,14],
  ideal3=responseprobs.Z[2:10,3,3,14] - responseprobs.Z[2:10,3,2,14],
  ideal4=responseprobs.Z[2:10,4,3,14] - responseprobs.Z[2:10,4,2,14],
  ideal5=responseprobs.Z[2:10,5,3,14] - responseprobs.Z[2:10,5,2,14])
#
responseprobs.healthinsurance.Pi4 <- data.frame(issue="healthinsurance", whichresponse="4", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,4,14] - responseprobs.Z[2:10,1,3,14],
  ideal2=responseprobs.Z[2:10,2,4,14] - responseprobs.Z[2:10,2,3,14],
  ideal3=responseprobs.Z[2:10,3,4,14] - responseprobs.Z[2:10,3,3,14],
  ideal4=responseprobs.Z[2:10,4,4,14] - responseprobs.Z[2:10,4,3,14],
  ideal5=responseprobs.Z[2:10,5,4,14] - responseprobs.Z[2:10,5,3,14])
#
responseprobs.healthinsurance.Pi5 <- data.frame(issue="healthinsurance", whichresponse="5", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,5,14] - responseprobs.Z[2:10,1,4,14],
  ideal2=responseprobs.Z[2:10,2,5,14] - responseprobs.Z[2:10,2,4,14],
  ideal3=responseprobs.Z[2:10,3,5,14] - responseprobs.Z[2:10,3,4,14],
  ideal4=responseprobs.Z[2:10,4,5,14] - responseprobs.Z[2:10,4,4,14],
  ideal5=responseprobs.Z[2:10,5,5,14] - responseprobs.Z[2:10,5,4,14])
#
responseprobs.healthinsurance.Pi6 <- data.frame(issue="healthinsurance", whichresponse="6", time=seq(1984,2016,by=4),
  ideal1=responseprobs.Z[2:10,1,6,14] - responseprobs.Z[2:10,1,5,14],
  ideal2=responseprobs.Z[2:10,2,6,14] - responseprobs.Z[2:10,2,5,14],
  ideal3=responseprobs.Z[2:10,3,6,14] - responseprobs.Z[2:10,3,5,14],
  ideal4=responseprobs.Z[2:10,4,6,14] - responseprobs.Z[2:10,4,5,14],
  ideal5=responseprobs.Z[2:10,5,6,14] - responseprobs.Z[2:10,5,5,14])
#
responseprobs.healthinsurance.Pi7 <- data.frame(issue="healthinsurance", whichresponse="7", time=seq(1984,2016,by=4),
  ideal1=1 - responseprobs.Z[2:10,1,6,14],
  ideal2=1 - responseprobs.Z[2:10,2,6,14],
  ideal3=1 - responseprobs.Z[2:10,3,6,14],
  ideal4=1 - responseprobs.Z[2:10,4,6,14],
  ideal5=1 - responseprobs.Z[2:10,5,6,14])
#
responseprobs.healthinsurance.combined <- rbind(responseprobs.healthinsurance.Pi1, responseprobs.healthinsurance.Pi2, responseprobs.healthinsurance.Pi3, responseprobs.healthinsurance.Pi4, responseprobs.healthinsurance.Pi5, responseprobs.healthinsurance.Pi6, responseprobs.healthinsurance.Pi7)
#
names(responseprobs.healthinsurance.combined)[which(names(responseprobs.healthinsurance.combined)=="ideal1")] <- "10%"
names(responseprobs.healthinsurance.combined)[which(names(responseprobs.healthinsurance.combined)=="ideal2")] <- "25%"
names(responseprobs.healthinsurance.combined)[which(names(responseprobs.healthinsurance.combined)=="ideal3")] <- "50%"
names(responseprobs.healthinsurance.combined)[which(names(responseprobs.healthinsurance.combined)=="ideal4")] <- "75%"
names(responseprobs.healthinsurance.combined)[which(names(responseprobs.healthinsurance.combined)=="ideal5")] <- "90%"
#
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="1"] <- "1: Government\ninsurance plan"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="2"] <- "2"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="3"] <- "3"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="4"] <- "4"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="5"] <- "5"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="6"] <- "6"
responseprobs.healthinsurance.combined$whichresponse[responseprobs.healthinsurance.combined$whichresponse=="7"] <- "7: Private\ninsurance plan"
#
responseprobs.healthinsurance.plotdf <- melt(responseprobs.healthinsurance.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.healthinsurance.plotdf$whichresponse <- fct_inorder(responseprobs.healthinsurance.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_healthinsurance.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.healthinsurance.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1984-2016\n") +
  labs(subtitle="Health insurance",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.immigrationlevel.Pi1 <- data.frame(issue="immigrationlevel", whichresponse="1", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,1,18],
  ideal2=responseprobs.Z[4:10,2,1,18],
  ideal3=responseprobs.Z[4:10,3,1,18],
  ideal4=responseprobs.Z[4:10,4,1,18],
  ideal5=responseprobs.Z[4:10,5,1,18])
#
responseprobs.immigrationlevel.Pi2 <- data.frame(issue="immigrationlevel", whichresponse="2", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,2,18] - responseprobs.Z[4:10,1,1,18],
  ideal2=responseprobs.Z[4:10,2,2,18] - responseprobs.Z[4:10,2,1,18],
  ideal3=responseprobs.Z[4:10,3,2,18] - responseprobs.Z[4:10,3,1,18],
  ideal4=responseprobs.Z[4:10,4,2,18] - responseprobs.Z[4:10,4,1,18],
  ideal5=responseprobs.Z[4:10,5,2,18] - responseprobs.Z[4:10,5,1,18])
#
responseprobs.immigrationlevel.Pi3 <- data.frame(issue="immigrationlevel", whichresponse="3", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,3,18] - responseprobs.Z[4:10,1,2,18],
  ideal2=responseprobs.Z[4:10,2,3,18] - responseprobs.Z[4:10,2,2,18],
  ideal3=responseprobs.Z[4:10,3,3,18] - responseprobs.Z[4:10,3,2,18],
  ideal4=responseprobs.Z[4:10,4,3,18] - responseprobs.Z[4:10,4,2,18],
  ideal5=responseprobs.Z[4:10,5,3,18] - responseprobs.Z[4:10,5,2,18])
#
responseprobs.immigrationlevel.Pi4 <- data.frame(issue="immigrationlevel", whichresponse="4", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,4,18] - responseprobs.Z[4:10,1,3,18],
  ideal2=responseprobs.Z[4:10,2,4,18] - responseprobs.Z[4:10,2,3,18],
  ideal3=responseprobs.Z[4:10,3,4,18] - responseprobs.Z[4:10,3,3,18],
  ideal4=responseprobs.Z[4:10,4,4,18] - responseprobs.Z[4:10,4,3,18],
  ideal5=responseprobs.Z[4:10,5,4,18] - responseprobs.Z[4:10,5,3,18])
#
responseprobs.immigrationlevel.Pi5 <- data.frame(issue="immigrationlevel", whichresponse="5", time=seq(1992,2016,by=4),
  ideal1=1 - responseprobs.Z[4:10,1,4,18],
  ideal2=1 - responseprobs.Z[4:10,2,4,18],
  ideal3=1 - responseprobs.Z[4:10,3,4,18],
  ideal4=1 - responseprobs.Z[4:10,4,4,18],
  ideal5=1 - responseprobs.Z[4:10,5,4,18])
#
responseprobs.immigrationlevel.combined <- rbind(responseprobs.immigrationlevel.Pi1, responseprobs.immigrationlevel.Pi2, responseprobs.immigrationlevel.Pi3, responseprobs.immigrationlevel.Pi4, responseprobs.immigrationlevel.Pi5)
#
names(responseprobs.immigrationlevel.combined)[which(names(responseprobs.immigrationlevel.combined)=="ideal1")] <- "10%"
names(responseprobs.immigrationlevel.combined)[which(names(responseprobs.immigrationlevel.combined)=="ideal2")] <- "25%"
names(responseprobs.immigrationlevel.combined)[which(names(responseprobs.immigrationlevel.combined)=="ideal3")] <- "50%"
names(responseprobs.immigrationlevel.combined)[which(names(responseprobs.immigrationlevel.combined)=="ideal4")] <- "75%"
names(responseprobs.immigrationlevel.combined)[which(names(responseprobs.immigrationlevel.combined)=="ideal5")] <- "90%"
#
responseprobs.immigrationlevel.combined$whichresponse[responseprobs.immigrationlevel.combined$whichresponse=="1"] <- "Increased a lot"
responseprobs.immigrationlevel.combined$whichresponse[responseprobs.immigrationlevel.combined$whichresponse=="2"] <- "Increased a little"
responseprobs.immigrationlevel.combined$whichresponse[responseprobs.immigrationlevel.combined$whichresponse=="3"] <- "Left the same\nas it is now"
responseprobs.immigrationlevel.combined$whichresponse[responseprobs.immigrationlevel.combined$whichresponse=="4"] <- "Decreased a little"
responseprobs.immigrationlevel.combined$whichresponse[responseprobs.immigrationlevel.combined$whichresponse=="5"] <- "Decreased a lot"
#
responseprobs.immigrationlevel.plotdf <- melt(responseprobs.immigrationlevel.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.immigrationlevel.plotdf$whichresponse <- fct_inorder(responseprobs.immigrationlevel.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_immigrationlevel.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.immigrationlevel.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1992-2016\n") +
  labs(subtitle="Immigration levels",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.poorspend.Pi1 <- data.frame(issue="poorspend", whichresponse="1", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,1,10],
  ideal2=responseprobs.Z[4:10,2,1,10],
  ideal3=responseprobs.Z[4:10,3,1,10],
  ideal4=responseprobs.Z[4:10,4,1,10],
  ideal5=responseprobs.Z[4:10,5,1,10])
#
responseprobs.poorspend.Pi2 <- data.frame(issue="poorspend", whichresponse="2", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,2,10] - responseprobs.Z[4:10,1,1,10],
  ideal2=responseprobs.Z[4:10,2,2,10] - responseprobs.Z[4:10,2,1,10],
  ideal3=responseprobs.Z[4:10,3,2,10] - responseprobs.Z[4:10,3,1,10],
  ideal4=responseprobs.Z[4:10,4,2,10] - responseprobs.Z[4:10,4,1,10],
  ideal5=responseprobs.Z[4:10,5,2,10] - responseprobs.Z[4:10,5,1,10])
#
responseprobs.poorspend.Pi3 <- data.frame(issue="poorspend", whichresponse="3", time=seq(1992,2016,by=4),
  ideal1=1 - responseprobs.Z[4:10,1,2,10],
  ideal2=1 - responseprobs.Z[4:10,2,2,10],
  ideal3=1 - responseprobs.Z[4:10,3,2,10],
  ideal4=1 - responseprobs.Z[4:10,4,2,10],
  ideal5=1 - responseprobs.Z[4:10,5,2,10])
#
responseprobs.poorspend.combined <- rbind(responseprobs.poorspend.Pi1, responseprobs.poorspend.Pi2, responseprobs.poorspend.Pi3)
#
names(responseprobs.poorspend.combined)[which(names(responseprobs.poorspend.combined)=="ideal1")] <- "10%"
names(responseprobs.poorspend.combined)[which(names(responseprobs.poorspend.combined)=="ideal2")] <- "25%"
names(responseprobs.poorspend.combined)[which(names(responseprobs.poorspend.combined)=="ideal3")] <- "50%"
names(responseprobs.poorspend.combined)[which(names(responseprobs.poorspend.combined)=="ideal4")] <- "75%"
names(responseprobs.poorspend.combined)[which(names(responseprobs.poorspend.combined)=="ideal5")] <- "90%"
#
responseprobs.poorspend.combined$whichresponse[responseprobs.poorspend.combined$whichresponse=="1"] <- "Increased"
responseprobs.poorspend.combined$whichresponse[responseprobs.poorspend.combined$whichresponse=="2"] <- "Kept the same"
responseprobs.poorspend.combined$whichresponse[responseprobs.poorspend.combined$whichresponse=="3"] <- "Decreased"
#
responseprobs.poorspend.plotdf <- melt(responseprobs.poorspend.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.poorspend.plotdf$whichresponse <- fct_inorder(responseprobs.poorspend.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_poorspend.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.poorspend.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1992-2016\n") +
  labs(subtitle="Federal spending on poor",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
responseprobs.welfarespend.Pi1 <- data.frame(issue="welfarespend", whichresponse="1", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,1,9],
  ideal2=responseprobs.Z[4:10,2,1,9],
  ideal3=responseprobs.Z[4:10,3,1,9],
  ideal4=responseprobs.Z[4:10,4,1,9],
  ideal5=responseprobs.Z[4:10,5,1,9])
#
responseprobs.welfarespend.Pi2 <- data.frame(issue="welfarespend", whichresponse="2", time=seq(1992,2016,by=4),
  ideal1=responseprobs.Z[4:10,1,2,9] - responseprobs.Z[4:10,1,1,9],
  ideal2=responseprobs.Z[4:10,2,2,9] - responseprobs.Z[4:10,2,1,9],
  ideal3=responseprobs.Z[4:10,3,2,9] - responseprobs.Z[4:10,3,1,9],
  ideal4=responseprobs.Z[4:10,4,2,9] - responseprobs.Z[4:10,4,1,9],
  ideal5=responseprobs.Z[4:10,5,2,9] - responseprobs.Z[4:10,5,1,9])
#
responseprobs.welfarespend.Pi3 <- data.frame(issue="welfarespend", whichresponse="3", time=seq(1992,2016,by=4),
  ideal1=1 - responseprobs.Z[4:10,1,2,9],
  ideal2=1 - responseprobs.Z[4:10,2,2,9],
  ideal3=1 - responseprobs.Z[4:10,3,2,9],
  ideal4=1 - responseprobs.Z[4:10,4,2,9],
  ideal5=1 - responseprobs.Z[4:10,5,2,9])
#
responseprobs.welfarespend.combined <- rbind(responseprobs.welfarespend.Pi1, responseprobs.welfarespend.Pi2, responseprobs.welfarespend.Pi3)
#
names(responseprobs.welfarespend.combined)[which(names(responseprobs.welfarespend.combined)=="ideal1")] <- "10%"
names(responseprobs.welfarespend.combined)[which(names(responseprobs.welfarespend.combined)=="ideal2")] <- "25%"
names(responseprobs.welfarespend.combined)[which(names(responseprobs.welfarespend.combined)=="ideal3")] <- "50%"
names(responseprobs.welfarespend.combined)[which(names(responseprobs.welfarespend.combined)=="ideal4")] <- "75%"
names(responseprobs.welfarespend.combined)[which(names(responseprobs.welfarespend.combined)=="ideal5")] <- "90%"
#
responseprobs.welfarespend.combined$whichresponse[responseprobs.welfarespend.combined$whichresponse=="1"] <- "Increased"
responseprobs.welfarespend.combined$whichresponse[responseprobs.welfarespend.combined$whichresponse=="2"] <- "Kept the same"
responseprobs.welfarespend.combined$whichresponse[responseprobs.welfarespend.combined$whichresponse=="3"] <- "Decreased"
#
responseprobs.welfarespend.plotdf <- melt(responseprobs.welfarespend.combined, id.vars=c("time","issue","whichresponse"))
responseprobs.welfarespend.plotdf$whichresponse <- fct_inorder(responseprobs.welfarespend.plotdf$whichresponse)
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_responseprobs_welfarespend.pdf", height=7.0, width=9.0)
#
ggplot(responseprobs.welfarespend.plotdf, aes(x=time, y=value, color=variable)) +
  geom_line(lwd=1.5) +
  facet_wrap(~factor(whichresponse), labeller = label_context) +
  scale_color_viridis(discrete=TRUE, begin=0.0, end=1.0, name="Ideology percentile") +
  xlab("\nYear") +
  ylab("Predicted probability of response from DO-IRT model\n") +
  ggtitle("American National Election Studies: 1992-2016\n") +
  labs(subtitle="Federal spending on welfare programs",
       caption="\nIdeology percentiles are based on respondent ideal points (higher values indicate more conservative opinions).") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
#
#
# Cut Points: gay discrimination
#
cutpoints1 <- cutpoints2 <- matrix(NA, nrow=nrow(alpha.samps[[1]]), ncol=ncol(alpha.samps[[1]]))
colnames(cutpoints1) <- colnames(cutpoints2) <- colnames(alpha.samps[[1]])
  for (j in 1:p){
    for (c in 1:(K[j]-1)){
      for (t in 1:T){
        cutpoints1[,paste("alpha[",j,",",c,",",t,"]",sep="")] <- alpha.samps[[1]][,paste("alpha[",j,",",c,",",t,"]",sep="")] / beta.samps[[1]][,paste("beta[",j,",",t,"]",sep="")]
        cutpoints2[,paste("alpha[",j,",",c,",",t,"]",sep="")] <- alpha.samps[[2]][,paste("alpha[",j,",",c,",",t,"]",sep="")] / beta.samps[[2]][,paste("beta[",j,",",t,"]",sep="")]
      }}}
#
cutpoints <- t(rbind(cutpoints1, cutpoints2))
#
cutpoints.gaydiscrimination <- na.omit(cutpoints[grepl("alpha\\[2,", rownames(cutpoints)),])
#
cutpoints.gaydiscrimination.summary <- data.frame(parameter=rownames(cutpoints.gaydiscrimination),
      Boundary=c(1:3),
      time=c(rep(1980,3), rep(1984,3), rep(1988,3), rep(1992,3), rep(1996,3), rep(2000,3), rep(2004,3), rep(2008,3), rep(2012,3), rep(2016,3)),
      mean=apply(cutpoints.gaydiscrimination, 1, mean),
      lower=apply(cutpoints.gaydiscrimination, 1, function(x) {quantile(x, 0.025)}),
      upper=apply(cutpoints.gaydiscrimination, 1, function(x) {quantile(x, 0.975)}))
#
cutpoints.gaydiscrimination.summary <- subset(cutpoints.gaydiscrimination.summary, time >= 1988)
#
cutpoints.gaydiscrimination.summary$Boundary <- factor(cutpoints.gaydiscrimination.summary$Boundary, labels=c("Favor strongly/Favor not strongly", "Favor not strongly/Oppose not strongly", "Oppose not strongly/Oppose strongly"))
cutpoints.gaydiscrimination.summary$Boundary <- factor(cutpoints.gaydiscrimination.summary$Boundary, levels=c("Oppose not strongly/Oppose strongly", "Favor not strongly/Oppose not strongly", "Favor strongly/Favor not strongly"))
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_cutpoints_gaydiscrimination.pdf", height=5.5, width=8.5)
#
ggplot(cutpoints.gaydiscrimination.summary, aes(x=time, y=mean, group=Boundary)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey70") +
  geom_line(aes(color=Boundary), lwd=1.5) +
  scale_color_viridis(option="A", direction=-1, discrete=TRUE, name="Boundaries between\nresponse options\n",
    labels=c("Oppose strongly/\nOppose not strongly\n","Oppose not strongly/\nFavor not strongly\n","Favor not strongly/\nFavor strongly")) +
  xlab("\nYear") +
  ylab(expression(atop("Ideological cut point (" *frac(alpha[jct],beta[jt]) *")",""))) +
  ggtitle("American National Election Studies: 1988-2016\n") +
  labs(subtitle="Do you favor or oppose laws to protect gays and lesbians against job discrimination?",
       caption="\nShaded regions show 95% credible intervals.") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
# Cut Points: abortion
#
cutpoints.abortion <- na.omit(cutpoints[grepl("alpha\\[1,", rownames(cutpoints)),])
#
cutpoints.abortion.summary <- data.frame(parameter=rownames(cutpoints.abortion),
      Boundary=c(1:3),
      time=c(rep(1980,3), rep(1984,3), rep(1988,3), rep(1992,3), rep(1996,3), rep(2000,3), rep(2004,3), rep(2008,3), rep(2012,3), rep(2016,3)),
      mean=apply(cutpoints.abortion, 1, mean),
      lower=apply(cutpoints.abortion, 1, function(x) {quantile(x, 0.025)}),
      upper=apply(cutpoints.abortion, 1, function(x) {quantile(x, 0.975)}))
#
cutpoints.abortion.summary <- subset(cutpoints.abortion.summary, time >= 1988)
#
# Truncate at -4/4
cutpoints.abortion.summary[,4:6] [cutpoints.abortion.summary[,4:6] > 4] <- 4
cutpoints.abortion.summary[,4:6] [cutpoints.abortion.summary[,4:6] < -4] <- -4
#
cutpoints.abortion.summary$Boundary <- factor(cutpoints.abortion.summary$Boundary, labels=c("Never permitted/Permitted only in special cases",
  "Permitted only in special cases/Allowed after need established", "Allowed after need established/Always able to obtain"))
#cutpoints.abortion.summary$Boundary <- factor(cutpoints.abortion.summary$Boundary, levels=c("Oppose not strongly/Oppose strongly", "Favor not strongly/Oppose not strongly", "Favor strongly/Favor not strongly"))
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_cutpoints_abortion.pdf", height=5.5, width=8.5)
#
ggplot(cutpoints.abortion.summary, aes(x=time, y=mean, group=Boundary)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey70") +
  geom_line(aes(color=Boundary), lwd=1.5) +
  scale_color_viridis(option="B", direction=-1, discrete=TRUE, name="Boundaries between\nresponse options\n",
  labels=c("Never permitted/\nPermitted only in special cases\n","Permitted only in special cases/\nAllowed after need established\n","Allowed after need established/\nAlways able to obtain")) +
  xlab("\nYear") +
  ylab(expression(atop("Ideological cut point (" *frac(alpha[jct],beta[jt]) *")",""))) +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(subtitle="There has been some discussion about abortion during recent years.\nWhich one of [these] opinions best agrees with your view?",
        caption="\nValues truncated at -4/4. Shaded regions show 95% credible intervals.") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
#
# Beta
#
beta_summary <- cbind(beta.summary[[1]][,1], beta.summary[[2]][,1], beta.summary[[2]][,5])
#
# Beta - Libcon
#
beta_libcon_1980 <- cbind(1980, beta_summary["beta[11,1]",1], beta_summary["beta[11,1]",2], beta_summary["beta[11,1]",3])
beta_libcon_1984 <- cbind(1984, beta_summary["beta[11,2]",1], beta_summary["beta[11,2]",2], beta_summary["beta[11,2]",3])
beta_libcon_1988 <- cbind(1988, beta_summary["beta[11,3]",1], beta_summary["beta[11,3]",2], beta_summary["beta[11,3]",3])
beta_libcon_1992 <- cbind(1992, beta_summary["beta[11,4]",1], beta_summary["beta[11,4]",2], beta_summary["beta[11,4]",3])
beta_libcon_1996 <- cbind(1996, beta_summary["beta[11,5]",1], beta_summary["beta[11,5]",2], beta_summary["beta[11,5]",3])
beta_libcon_2000 <- cbind(2000, beta_summary["beta[11,6]",1], beta_summary["beta[11,6]",2], beta_summary["beta[11,6]",3])
beta_libcon_2004 <- cbind(2004, beta_summary["beta[11,7]",1], beta_summary["beta[11,7]",2], beta_summary["beta[11,7]",3])
beta_libcon_2008 <- cbind(2008, beta_summary["beta[11,8]",1], beta_summary["beta[11,8]",2], beta_summary["beta[11,8]",3])
beta_libcon_2012 <- cbind(2012, beta_summary["beta[11,9]",1], beta_summary["beta[11,9]",2], beta_summary["beta[11,9]",3])
beta_libcon_2016 <- cbind(2016, beta_summary["beta[11,10]",1], beta_summary["beta[11,10]",2], beta_summary["beta[11,10]",3])
#
beta_libcon <- data.frame(cbind(rep("Liberal-Conservative ID",10), rbind(beta_libcon_1980, beta_libcon_1984, beta_libcon_1988, beta_libcon_1992,
	beta_libcon_1996, beta_libcon_2000, beta_libcon_2004, beta_libcon_2008, beta_libcon_2012, beta_libcon_2016)))
#
#
# Beta - Abortion
#
beta_abortion_1980 <- cbind(1980, beta_summary["beta[1,1]",1], beta_summary["beta[1,1]",2], beta_summary["beta[1,1]",3])
beta_abortion_1984 <- cbind(1984, beta_summary["beta[1,2]",1], beta_summary["beta[1,2]",2], beta_summary["beta[1,2]",3])
beta_abortion_1988 <- cbind(1988, beta_summary["beta[1,3]",1], beta_summary["beta[1,3]",2], beta_summary["beta[1,3]",3])
beta_abortion_1992 <- cbind(1992, beta_summary["beta[1,4]",1], beta_summary["beta[1,4]",2], beta_summary["beta[1,4]",3])
beta_abortion_1996 <- cbind(1996, beta_summary["beta[1,5]",1], beta_summary["beta[1,5]",2], beta_summary["beta[1,5]",3])
beta_abortion_2000 <- cbind(2000, beta_summary["beta[1,6]",1], beta_summary["beta[1,6]",2], beta_summary["beta[1,6]",3])
beta_abortion_2004 <- cbind(2004, beta_summary["beta[1,7]",1], beta_summary["beta[1,7]",2], beta_summary["beta[1,7]",3])
beta_abortion_2008 <- cbind(2008, beta_summary["beta[1,8]",1], beta_summary["beta[1,8]",2], beta_summary["beta[1,8]",3])
beta_abortion_2012 <- cbind(2012, beta_summary["beta[1,9]",1], beta_summary["beta[1,9]",2], beta_summary["beta[1,9]",3])
beta_abortion_2016 <- cbind(2016, beta_summary["beta[1,10]",1], beta_summary["beta[1,10]",2], beta_summary["beta[1,10]",3])
#
beta_abortion <- data.frame(cbind(rep("Abortion",10), rbind(beta_abortion_1980, beta_abortion_1984, beta_abortion_1988, beta_abortion_1992,
	beta_abortion_1996, beta_abortion_2000, beta_abortion_2004, beta_abortion_2008, beta_abortion_2012, beta_abortion_2016)))
#
#
# Beta - Gay Discrimination
#
beta_gaydisc_1980 <- cbind(1980, NA, NA, NA)
beta_gaydisc_1984 <- cbind(1984, NA, NA, NA)
beta_gaydisc_1988 <- cbind(1988, beta_summary["beta[2,3]",1], beta_summary["beta[2,3]",2], beta_summary["beta[2,3]",3])
beta_gaydisc_1992 <- cbind(1992, beta_summary["beta[2,4]",1], beta_summary["beta[2,4]",2], beta_summary["beta[2,4]",3])
beta_gaydisc_1996 <- cbind(1996, beta_summary["beta[2,5]",1], beta_summary["beta[2,5]",2], beta_summary["beta[2,5]",3])
beta_gaydisc_2000 <- cbind(2000, beta_summary["beta[2,6]",1], beta_summary["beta[2,6]",2], beta_summary["beta[2,6]",3])
beta_gaydisc_2004 <- cbind(2004, beta_summary["beta[2,7]",1], beta_summary["beta[2,7]",2], beta_summary["beta[2,7]",3])
beta_gaydisc_2008 <- cbind(2008, beta_summary["beta[2,8]",1], beta_summary["beta[2,8]",2], beta_summary["beta[2,8]",3])
beta_gaydisc_2012 <- cbind(2012, beta_summary["beta[2,9]",1], beta_summary["beta[2,9]",2], beta_summary["beta[2,9]",3])
beta_gaydisc_2016 <- cbind(2016, beta_summary["beta[2,10]",1], beta_summary["beta[2,10]",2], beta_summary["beta[2,10]",3])
#
beta_gaydisc <- data.frame(cbind(rep("Gay Discrimination",10), rbind(beta_gaydisc_1980, beta_gaydisc_1984, beta_gaydisc_1988, beta_gaydisc_1992,
	beta_gaydisc_1996, beta_gaydisc_2000, beta_gaydisc_2004, beta_gaydisc_2008, beta_gaydisc_2012, beta_gaydisc_2016)))
#
#
# Beta - Gay Adoption
#
beta_gayadopt_1980 <- cbind(1980, NA, NA, NA)
beta_gayadopt_1984 <- cbind(1984, NA, NA, NA)
beta_gayadopt_1988 <- cbind(1988, NA, NA, NA)
beta_gayadopt_1992 <- cbind(1992, beta_summary["beta[4,4]",1], beta_summary["beta[4,4]",2], beta_summary["beta[4,4]",3])
beta_gayadopt_1996 <- cbind(1996, NA, NA, NA)
beta_gayadopt_2000 <- cbind(2000, beta_summary["beta[3,6]",1], beta_summary["beta[3,6]",2], beta_summary["beta[3,6]",3])
beta_gayadopt_2004 <- cbind(2004, beta_summary["beta[3,7]",1], beta_summary["beta[3,7]",2], beta_summary["beta[3,7]",3])
beta_gayadopt_2008 <- cbind(2008, beta_summary["beta[3,8]",1], beta_summary["beta[3,8]",2], beta_summary["beta[3,8]",3])
beta_gayadopt_2012 <- cbind(2012, beta_summary["beta[3,9]",1], beta_summary["beta[3,9]",2], beta_summary["beta[3,9]",3])
beta_gayadopt_2016 <- cbind(2016, beta_summary["beta[3,10]",1], beta_summary["beta[3,10]",2], beta_summary["beta[3,10]",3])
#
beta_gayadopt <- data.frame(cbind(rep("Gay Adoption",10), rbind(beta_gayadopt_1980, beta_gayadopt_1984, beta_gayadopt_1988, beta_gayadopt_1992,
	beta_gayadopt_1996, beta_gayadopt_2000, beta_gayadopt_2004, beta_gayadopt_2008, beta_gayadopt_2012, beta_gayadopt_2016)))
#
#
# Beta - Gay Marriage
#
beta_gaymarriage_1980 <- cbind(1980, NA, NA, NA)
beta_gaymarriage_1984 <- cbind(1984, NA, NA, NA)
beta_gaymarriage_1988 <- cbind(1988, NA, NA, NA)
beta_gaymarriage_1992 <- cbind(1992, NA, NA, NA)
beta_gaymarriage_1996 <- cbind(1996, NA, NA, NA)
beta_gaymarriage_2000 <- cbind(2000, NA, NA, NA)
beta_gaymarriage_2004 <- cbind(2004, beta_summary["beta[5,7]",1], beta_summary["beta[5,7]",2], beta_summary["beta[5,7]",3])
beta_gaymarriage_2008 <- cbind(2008, beta_summary["beta[5,8]",1], beta_summary["beta[5,8]",2], beta_summary["beta[5,8]",3])
beta_gaymarriage_2012 <- cbind(2012, beta_summary["beta[5,9]",1], beta_summary["beta[5,9]",2], beta_summary["beta[5,9]",3])
beta_gaymarriage_2016 <- cbind(2016, beta_summary["beta[5,10]",1], beta_summary["beta[5,10]",2], beta_summary["beta[5,10]",3])
#
beta_gaymarriage <- data.frame(cbind(rep("Gay Marriage",10), rbind(beta_gaymarriage_1980, beta_gaymarriage_1984, beta_gaymarriage_1988, beta_gaymarriage_1992,
	beta_gaymarriage_1996, beta_gaymarriage_2000, beta_gaymarriage_2004, beta_gaymarriage_2008, beta_gaymarriage_2012, beta_gaymarriage_2016)))
#
#
# Beta - Gun Control
#
beta_guncontrol_1980 <- cbind(1980, NA, NA, NA)
beta_guncontrol_1984 <- cbind(1984, NA, NA, NA)
beta_guncontrol_1988 <- cbind(1988, NA, NA, NA)
beta_guncontrol_1992 <- cbind(1992, NA, NA, NA)
beta_guncontrol_1996 <- cbind(1996, beta_summary["beta[6,5]",1], beta_summary["beta[6,5]",2], beta_summary["beta[6,5]",3])
beta_guncontrol_2000 <- cbind(2000, beta_summary["beta[8,6]",1], beta_summary["beta[8,6]",2], beta_summary["beta[8,6]",3])
beta_guncontrol_2004 <- cbind(2004, beta_summary["beta[8,7]",1], beta_summary["beta[8,7]",2], beta_summary["beta[8,7]",3])
beta_guncontrol_2008 <- cbind(2008, beta_summary["beta[7,8]",1], beta_summary["beta[7,8]",2], beta_summary["beta[7,8]",3])
beta_guncontrol_2012 <- cbind(2012, beta_summary["beta[7,9]",1], beta_summary["beta[7,9]",2], beta_summary["beta[7,9]",3])
beta_guncontrol_2016 <- cbind(2016, beta_summary["beta[7,10]",1], beta_summary["beta[7,10]",2], beta_summary["beta[7,10]",3])
#
beta_guncontrol <- data.frame(cbind(rep("Gun Control",10), rbind(beta_guncontrol_1980, beta_guncontrol_1984, beta_guncontrol_1988, beta_guncontrol_1992,
	beta_guncontrol_1996, beta_guncontrol_2000, beta_guncontrol_2004, beta_guncontrol_2008, beta_guncontrol_2012, beta_guncontrol_2016)))
#
#
# Beta - Environment-Jobs
#
beta_envjobs_1980 <- cbind(1980, NA, NA, NA)
beta_envjobs_1984 <- cbind(1984, NA, NA, NA)
beta_envjobs_1988 <- cbind(1988, NA, NA, NA)
beta_envjobs_1992 <- cbind(1992, NA, NA, NA)
beta_envjobs_1996 <- cbind(1996, beta_summary["beta[15,5]",1], beta_summary["beta[15,5]",2], beta_summary["beta[15,5]",3])
beta_envjobs_2000 <- cbind(2000, beta_summary["beta[15,6]",1], beta_summary["beta[15,6]",2], beta_summary["beta[15,6]",3])
beta_envjobs_2004 <- cbind(2004, beta_summary["beta[15,7]",1], beta_summary["beta[15,7]",2], beta_summary["beta[15,7]",3])
beta_envjobs_2008 <- cbind(2008, beta_summary["beta[15,8]",1], beta_summary["beta[15,8]",2], beta_summary["beta[15,8]",3])
beta_envjobs_2012 <- cbind(2012, beta_summary["beta[15,9]",1], beta_summary["beta[15,9]",2], beta_summary["beta[15,9]",3])
beta_envjobs_2016 <- cbind(2016, beta_summary["beta[15,10]",1], beta_summary["beta[15,10]",2], beta_summary["beta[15,10]",3])
#
beta_envjobs <- data.frame(cbind(rep("Environment-Jobs",10), rbind(beta_envjobs_1980, beta_envjobs_1984, beta_envjobs_1988, beta_envjobs_1992,
	beta_envjobs_1996, beta_envjobs_2000, beta_envjobs_2004, beta_envjobs_2008, beta_envjobs_2012, beta_envjobs_2016)))
#
#
# Beta - Government Spending and Services
#
beta_govtspend_1980 <- cbind(1980, beta_summary["beta[12,1]",1], beta_summary["beta[12,1]",2], beta_summary["beta[12,1]",3])
beta_govtspend_1984 <- cbind(1984, beta_summary["beta[12,2]",1], beta_summary["beta[12,2]",2], beta_summary["beta[12,2]",3])
beta_govtspend_1988 <- cbind(1988, beta_summary["beta[12,3]",1], beta_summary["beta[12,3]",2], beta_summary["beta[12,3]",3])
beta_govtspend_1992 <- cbind(1992, beta_summary["beta[12,4]",1], beta_summary["beta[12,4]",2], beta_summary["beta[12,4]",3])
beta_govtspend_1996 <- cbind(1996, beta_summary["beta[12,5]",1], beta_summary["beta[12,5]",2], beta_summary["beta[12,5]",3])
beta_govtspend_2000 <- cbind(2000, beta_summary["beta[12,6]",1], beta_summary["beta[12,6]",2], beta_summary["beta[12,6]",3])
beta_govtspend_2004 <- cbind(2004, beta_summary["beta[12,7]",1], beta_summary["beta[12,7]",2], beta_summary["beta[12,7]",3])
beta_govtspend_2008 <- cbind(2008, beta_summary["beta[12,8]",1], beta_summary["beta[12,8]",2], beta_summary["beta[12,8]",3])
beta_govtspend_2012 <- cbind(2012, beta_summary["beta[12,9]",1], beta_summary["beta[12,9]",2], beta_summary["beta[12,9]",3])
beta_govtspend_2016 <- cbind(2016, beta_summary["beta[12,10]",1], beta_summary["beta[12,10]",2], beta_summary["beta[12,10]",3])
#
beta_govtspend <- data.frame(cbind(rep("Govt Spending and Services",10), rbind(beta_govtspend_1980, beta_govtspend_1984, beta_govtspend_1988, beta_govtspend_1992,
	beta_govtspend_1996, beta_govtspend_2000, beta_govtspend_2004, beta_govtspend_2008, beta_govtspend_2012, beta_govtspend_2016)))
#
#
# Beta - Guaranteed Jobs
#
beta_guarjobs_1980 <- cbind(1980, beta_summary["beta[13,1]",1], beta_summary["beta[13,1]",2], beta_summary["beta[13,1]",3])
beta_guarjobs_1984 <- cbind(1984, beta_summary["beta[13,2]",1], beta_summary["beta[13,2]",2], beta_summary["beta[13,2]",3])
beta_guarjobs_1988 <- cbind(1988, beta_summary["beta[13,3]",1], beta_summary["beta[13,3]",2], beta_summary["beta[13,3]",3])
beta_guarjobs_1992 <- cbind(1992, beta_summary["beta[13,4]",1], beta_summary["beta[13,4]",2], beta_summary["beta[13,4]",3])
beta_guarjobs_1996 <- cbind(1996, beta_summary["beta[13,5]",1], beta_summary["beta[13,5]",2], beta_summary["beta[13,5]",3])
beta_guarjobs_2000 <- cbind(2000, beta_summary["beta[13,6]",1], beta_summary["beta[13,6]",2], beta_summary["beta[13,6]",3])
beta_guarjobs_2004 <- cbind(2004, beta_summary["beta[13,7]",1], beta_summary["beta[13,7]",2], beta_summary["beta[13,7]",3])
beta_guarjobs_2008 <- cbind(2008, beta_summary["beta[13,8]",1], beta_summary["beta[13,8]",2], beta_summary["beta[13,8]",3])
beta_guarjobs_2012 <- cbind(2012, beta_summary["beta[13,9]",1], beta_summary["beta[13,9]",2], beta_summary["beta[13,9]",3])
beta_guarjobs_2016 <- cbind(2016, beta_summary["beta[13,10]",1], beta_summary["beta[13,10]",2], beta_summary["beta[13,10]",3])
#
beta_guarjobs <- data.frame(cbind(rep("Guaranteed Jobs",10), rbind(beta_guarjobs_1980, beta_guarjobs_1984, beta_guarjobs_1988, beta_guarjobs_1992,
	beta_guarjobs_1996, beta_guarjobs_2000, beta_guarjobs_2004, beta_guarjobs_2008, beta_guarjobs_2012, beta_guarjobs_2016)))
#
#
# Beta - Health Insurance
#
beta_healthins_1980 <- cbind(1980, NA, NA, NA)
beta_healthins_1984 <- cbind(1984, beta_summary["beta[14,2]",1], beta_summary["beta[14,2]",2], beta_summary["beta[14,2]",3])
beta_healthins_1988 <- cbind(1988, beta_summary["beta[14,3]",1], beta_summary["beta[14,3]",2], beta_summary["beta[14,3]",3])
beta_healthins_1992 <- cbind(1992, beta_summary["beta[14,4]",1], beta_summary["beta[14,4]",2], beta_summary["beta[14,4]",3])
beta_healthins_1996 <- cbind(1996, beta_summary["beta[14,5]",1], beta_summary["beta[14,5]",2], beta_summary["beta[14,5]",3])
beta_healthins_2000 <- cbind(2000, beta_summary["beta[14,6]",1], beta_summary["beta[14,6]",2], beta_summary["beta[14,6]",3])
beta_healthins_2004 <- cbind(2004, beta_summary["beta[14,7]",1], beta_summary["beta[14,7]",2], beta_summary["beta[14,7]",3])
beta_healthins_2008 <- cbind(2008, beta_summary["beta[14,8]",1], beta_summary["beta[14,8]",2], beta_summary["beta[14,8]",3])
beta_healthins_2012 <- cbind(2012, beta_summary["beta[14,9]",1], beta_summary["beta[14,9]",2], beta_summary["beta[14,9]",3])
beta_healthins_2016 <- cbind(2016, beta_summary["beta[14,10]",1], beta_summary["beta[14,10]",2], beta_summary["beta[14,10]",3])
#
beta_healthins <- data.frame(cbind(rep("Health Insurance",10), rbind(beta_healthins_1980, beta_healthins_1984, beta_healthins_1988, beta_healthins_1992,
	beta_healthins_1996, beta_healthins_2000, beta_healthins_2004, beta_healthins_2008, beta_healthins_2012, beta_healthins_2016)))
#
#
# Beta - Defense Spending
#
beta_defspend_1980 <- cbind(1980, beta_summary["beta[17,1]",1], beta_summary["beta[17,1]",2], beta_summary["beta[17,1]",3])
beta_defspend_1984 <- cbind(1984, beta_summary["beta[17,2]",1], beta_summary["beta[17,2]",2], beta_summary["beta[17,2]",3])
beta_defspend_1988 <- cbind(1988, beta_summary["beta[17,3]",1], beta_summary["beta[17,3]",2], beta_summary["beta[17,3]",3])
beta_defspend_1992 <- cbind(1992, beta_summary["beta[17,4]",1], beta_summary["beta[17,4]",2], beta_summary["beta[17,4]",3])
beta_defspend_1996 <- cbind(1996, beta_summary["beta[17,5]",1], beta_summary["beta[17,5]",2], beta_summary["beta[17,5]",3])
beta_defspend_2000 <- cbind(2000, beta_summary["beta[17,6]",1], beta_summary["beta[17,6]",2], beta_summary["beta[17,6]",3])
beta_defspend_2004 <- cbind(2004, beta_summary["beta[17,7]",1], beta_summary["beta[17,7]",2], beta_summary["beta[17,7]",3])
beta_defspend_2008 <- cbind(2008, beta_summary["beta[17,8]",1], beta_summary["beta[17,8]",2], beta_summary["beta[17,8]",3])
beta_defspend_2012 <- cbind(2012, beta_summary["beta[17,9]",1], beta_summary["beta[17,9]",2], beta_summary["beta[17,9]",3])
beta_defspend_2016 <- cbind(2016, beta_summary["beta[17,10]",1], beta_summary["beta[17,10]",2], beta_summary["beta[17,10]",3])
#
beta_defspend <- data.frame(cbind(rep("Defense Spending",10), rbind(beta_defspend_1980, beta_defspend_1984, beta_defspend_1988, beta_defspend_1992,
	beta_defspend_1996, beta_defspend_2000, beta_defspend_2004, beta_defspend_2008, beta_defspend_2012, beta_defspend_2016)))
#
#
# Beta - Aid to Blacks
#
beta_aidblacks_1980 <- cbind(1980, beta_summary["beta[16,1]",1], beta_summary["beta[16,1]",2], beta_summary["beta[16,1]",3])
beta_aidblacks_1984 <- cbind(1984, beta_summary["beta[16,2]",1], beta_summary["beta[16,2]",2], beta_summary["beta[16,2]",3])
beta_aidblacks_1988 <- cbind(1988, beta_summary["beta[16,3]",1], beta_summary["beta[16,3]",2], beta_summary["beta[16,3]",3])
beta_aidblacks_1992 <- cbind(1992, beta_summary["beta[16,4]",1], beta_summary["beta[16,4]",2], beta_summary["beta[16,4]",3])
beta_aidblacks_1996 <- cbind(1996, beta_summary["beta[16,5]",1], beta_summary["beta[16,5]",2], beta_summary["beta[16,5]",3])
beta_aidblacks_2000 <- cbind(2000, beta_summary["beta[16,6]",1], beta_summary["beta[16,6]",2], beta_summary["beta[16,6]",3])
beta_aidblacks_2004 <- cbind(2004, beta_summary["beta[16,7]",1], beta_summary["beta[16,7]",2], beta_summary["beta[16,7]",3])
beta_aidblacks_2008 <- cbind(2008, beta_summary["beta[16,8]",1], beta_summary["beta[16,8]",2], beta_summary["beta[16,8]",3])
beta_aidblacks_2012 <- cbind(2012, beta_summary["beta[16,9]",1], beta_summary["beta[16,9]",2], beta_summary["beta[16,9]",3])
beta_aidblacks_2016 <- cbind(2016, beta_summary["beta[16,10]",1], beta_summary["beta[16,10]",2], beta_summary["beta[16,10]",3])
#
beta_aidblacks <- data.frame(cbind(rep("Aid to Blacks",10), rbind(beta_aidblacks_1980, beta_aidblacks_1984, beta_aidblacks_1988, beta_aidblacks_1992,
	beta_aidblacks_1996, beta_aidblacks_2000, beta_aidblacks_2004, beta_aidblacks_2008, beta_aidblacks_2012, beta_aidblacks_2016)))
#
#
# Beta - Welfare Spending
#
beta_welfarespend_1980 <- cbind(1980, NA, NA, NA)
beta_welfarespend_1984 <- cbind(1984, NA, NA, NA)
beta_welfarespend_1988 <- cbind(1988, NA, NA, NA)
beta_welfarespend_1992 <- cbind(1992, beta_summary["beta[9,4]",1], beta_summary["beta[9,4]",2], beta_summary["beta[9,4]",3])
beta_welfarespend_1996 <- cbind(1996, beta_summary["beta[9,5]",1], beta_summary["beta[9,5]",2], beta_summary["beta[9,5]",3])
beta_welfarespend_2000 <- cbind(2000, beta_summary["beta[9,6]",1], beta_summary["beta[9,6]",2], beta_summary["beta[9,6]",3])
beta_welfarespend_2004 <- cbind(2004, beta_summary["beta[9,7]",1], beta_summary["beta[9,7]",2], beta_summary["beta[9,7]",3])
beta_welfarespend_2008 <- cbind(2008, beta_summary["beta[9,8]",1], beta_summary["beta[9,8]",2], beta_summary["beta[9,8]",3])
beta_welfarespend_2012 <- cbind(2012, beta_summary["beta[9,9]",1], beta_summary["beta[9,9]",2], beta_summary["beta[9,9]",3])
beta_welfarespend_2016 <- cbind(2016, beta_summary["beta[9,10]",1], beta_summary["beta[9,10]",2], beta_summary["beta[9,10]",3])
#
beta_welfarespend <- data.frame(cbind(rep("Welfare Spending",10), rbind(beta_welfarespend_1980, beta_welfarespend_1984, beta_welfarespend_1988, beta_welfarespend_1992,
	beta_welfarespend_1996, beta_welfarespend_2000, beta_welfarespend_2004, beta_welfarespend_2008, beta_welfarespend_2012, beta_welfarespend_2016)))
#
#
# Beta - Poor Spending
#
beta_poorspend_1980 <- cbind(1980, NA, NA, NA)
beta_poorspend_1984 <- cbind(1984, NA, NA, NA)
beta_poorspend_1988 <- cbind(1988, NA, NA, NA)
beta_poorspend_1992 <- cbind(1992, beta_summary["beta[10,4]",1], beta_summary["beta[10,4]",2], beta_summary["beta[10,4]",3])
beta_poorspend_1996 <- cbind(1996, beta_summary["beta[10,5]",1], beta_summary["beta[10,5]",2], beta_summary["beta[10,5]",3])
beta_poorspend_2000 <- cbind(2000, beta_summary["beta[10,6]",1], beta_summary["beta[10,6]",2], beta_summary["beta[10,6]",3])
beta_poorspend_2004 <- cbind(2004, beta_summary["beta[10,7]",1], beta_summary["beta[10,7]",2], beta_summary["beta[10,7]",3])
beta_poorspend_2008 <- cbind(2008, beta_summary["beta[10,8]",1], beta_summary["beta[10,8]",2], beta_summary["beta[10,8]",3])
beta_poorspend_2012 <- cbind(2012, beta_summary["beta[10,9]",1], beta_summary["beta[10,9]",2], beta_summary["beta[10,9]",3])
beta_poorspend_2016 <- cbind(2016, beta_summary["beta[10,10]",1], beta_summary["beta[10,10]",2], beta_summary["beta[10,10]",3])
#
beta_poorspend <- data.frame(cbind(rep("Poor Spending",10), rbind(beta_poorspend_1980, beta_poorspend_1984, beta_poorspend_1988, beta_poorspend_1992,
	beta_poorspend_1996, beta_poorspend_2000, beta_poorspend_2004, beta_poorspend_2008, beta_poorspend_2012, beta_poorspend_2016)))
#
#
# Beta - Immigration
#
beta_immigration_1980 <- cbind(1980, NA, NA, NA)
beta_immigration_1984 <- cbind(1984, NA, NA, NA)
beta_immigration_1988 <- cbind(1988, NA, NA, NA)
beta_immigration_1992 <- cbind(1992, beta_summary["beta[18,4]",1], beta_summary["beta[18,4]",2], beta_summary["beta[18,4]",3])
beta_immigration_1996 <- cbind(1996, beta_summary["beta[18,5]",1], beta_summary["beta[18,5]",2], beta_summary["beta[18,5]",3])
beta_immigration_2000 <- cbind(2000, beta_summary["beta[18,6]",1], beta_summary["beta[18,6]",2], beta_summary["beta[18,6]",3])
beta_immigration_2004 <- cbind(2004, beta_summary["beta[18,7]",1], beta_summary["beta[18,7]",2], beta_summary["beta[18,7]",3])
beta_immigration_2008 <- cbind(2008, beta_summary["beta[18,8]",1], beta_summary["beta[18,8]",2], beta_summary["beta[18,8]",3])
beta_immigration_2012 <- cbind(2012, beta_summary["beta[18,9]",1], beta_summary["beta[18,9]",2], beta_summary["beta[18,9]",3])
beta_immigration_2016 <- cbind(2016, beta_summary["beta[18,10]",1], beta_summary["beta[18,10]",2], beta_summary["beta[18,10]",3])
#
beta_immigration <- data.frame(cbind(rep("Immigration",10), rbind(beta_immigration_1980, beta_immigration_1984, beta_immigration_1988, beta_immigration_1992,
	beta_immigration_1996, beta_immigration_2000, beta_immigration_2004, beta_immigration_2008, beta_immigration_2012, beta_immigration_2016)))
#
#
#
#
# Combine Betas
#
betamat <- rbind(beta_libcon, beta_abortion, beta_gaydisc, beta_gayadopt, beta_gaymarriage,
  beta_guncontrol, beta_envjobs, beta_govtspend, beta_guarjobs, beta_healthins,
  beta_defspend, beta_aidblacks, beta_welfarespend, beta_poorspend, beta_immigration)
#
names(betamat) <- c("issue","year","betapoint","betalow","betahigh")
#
betamat$year <- as.numeric(as.character(betamat$year))
betamat$betapoint <- as.numeric(as.character(betamat$betapoint))
betamat$betalow <- as.numeric(as.character(betamat$betalow))
betamat$betahigh <- as.numeric(as.character(betamat$betahigh))
#
# Make sure all discrimination parameters are positive (absolute values)
for (i in 1:nrow(betamat)){
    if (!is.na(betamat$betapoint[i]) & betamat$betapoint[i] < 0) {betamat[i,3:5] <- -1 * betamat[i,3:5]}
}
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_discparams.pdf", height=9.5, width=7.0)
#
ggplot(na.omit(betamat), aes(x=year, y=betapoint)) +
  geom_pointrange(aes(ymin = betalow, ymax = betahigh), fill='gray85', color='black', shape=21, fatten=2.5, size=0.65) +
  facet_wrap(~issue, nrow=5) +
  theme_bw() +
  xlab("\n Year") +
  ylab(expression(atop("Discrimination parameter (" *beta[jt] *")",""))) +
  ggtitle("American National Election Studies: 1980-2016\n") +
  labs(caption="\nBars show 95% credible intervals.") +
  theme(plot.title = element_text(hjust = 0.5))
#
dev.off()
#
#
#
# Tracking mass ideological polarization
#
polarization.df <- data.frame(idealpoint=scale(x.idealpoints),
  sophistication=factor(sophis, labels=c("low","mid","high")),
  partyid=party7[!is.na(sophis2)],
  time=time[!is.na(sophis2)])
#
polarization.df$party3 <- NA
polarization.df$party3[polarization.df$partyid<=3] <- "Democrats"
polarization.df$party3[polarization.df$partyid>=5] <- "Republicans"
#
polarization.df2 <- na.omit(polarization.df) %>%
  dplyr::group_by(party3,time) %>%
  dplyr::summarize(median = quantile(idealpoint, probs = c(0.5)), lower = quantile(idealpoint, probs = c(0.1)), upper = quantile(idealpoint, probs = c(0.9)))
polarization.df2$year <- rep(seq(1980,2016,by=4),2)
#
polarization.df2$party3 <- factor(polarization.df2$party3, levels=c("Republicans", "Democrats"))
#
#
pdf("Dropbox/mass_constraint/images/DOIRT_masspolarization.pdf", height=6.2, width=6.0)
#
ggplot(na.omit(polarization.df2), aes(x=year)) +
  geom_ribbon(aes(ymin=lower, ymax=upper, fill=party3), alpha=0.2) +
  geom_line(aes(y=median, color=party3), lwd=2) +
  scale_color_viridis(option="C", begin=0.2, end=0.8, discrete=TRUE) +
  scale_fill_viridis(option="C", begin=0.2, end=0.8, discrete=TRUE) +
  xlab("\nYear") +
  ylab(expression(atop("Ideological ideal point (" *theta[i] *")",""))) +
  ggtitle("American National Election Studies: 1980-2016") +
  labs(subtitle="Party medians with 10-90% bounds\n", caption="\nIncludes party identifiers and leaners.") +
  theme(plot.title=element_text(hjust = 0.5), plot.subtitle=element_text(hjust = 0.5), legend.position="top", legend.box="horizontal", legend.title=element_blank())
#
dev.off()
#
#
#
# CORE VALUES AND PARTY ID
#
values.df <- data.frame(idealpoint=scale(x.idealpoints),
  sophistication=factor(sophis, labels=c("low","mid","high")),
  partyid=scale(party7[!is.na(sophis2)]),
  moraltraditionalism=scale(moraltraditionalism[!is.na(sophis2)]),
  egalitarianism=scale(egalitarianism[!is.na(sophis2)]),
  time=time[!is.na(sophis2)])
#
values.df <- na.omit(values.df)
#
# R2 values (model fits, regressing ideal points on values and party)
#
ols.partyonly.low.1988 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==3))
ols.partyonly.low.1992 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==4))
ols.partyonly.low.1996 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==5))
ols.partyonly.low.2000 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==6))
ols.partyonly.low.2004 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==7))
ols.partyonly.low.2008 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==8))
ols.partyonly.low.2012 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==9))
ols.partyonly.low.2016 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="low" & time==10))
#
ols.valuesonly.low.1988 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==3))
ols.valuesonly.low.1992 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==4))
ols.valuesonly.low.1996 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==5))
ols.valuesonly.low.2000 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==6))
ols.valuesonly.low.2004 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==7))
ols.valuesonly.low.2008 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==8))
ols.valuesonly.low.2012 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==9))
ols.valuesonly.low.2016 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==10))
#
ols.partyvalues.low.1988 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==3))
ols.partyvalues.low.1992 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==4))
ols.partyvalues.low.1996 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==5))
ols.partyvalues.low.2000 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==6))
ols.partyvalues.low.2004 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==7))
ols.partyvalues.low.2008 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==8))
ols.partyvalues.low.2012 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==9))
ols.partyvalues.low.2016 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="low" & time==10))
#
ols.partyonly.mid.1988 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==3))
ols.partyonly.mid.1992 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==4))
ols.partyonly.mid.1996 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==5))
ols.partyonly.mid.2000 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==6))
ols.partyonly.mid.2004 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==7))
ols.partyonly.mid.2008 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==8))
ols.partyonly.mid.2012 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==9))
ols.partyonly.mid.2016 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="mid" & time==10))
#
ols.valuesonly.mid.1988 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==3))
ols.valuesonly.mid.1992 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==4))
ols.valuesonly.mid.1996 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==5))
ols.valuesonly.mid.2000 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==6))
ols.valuesonly.mid.2004 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==7))
ols.valuesonly.mid.2008 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==8))
ols.valuesonly.mid.2012 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==9))
ols.valuesonly.mid.2016 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==10))
#
ols.partyvalues.mid.1988 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==3))
ols.partyvalues.mid.1992 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==4))
ols.partyvalues.mid.1996 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==5))
ols.partyvalues.mid.2000 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==6))
ols.partyvalues.mid.2004 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==7))
ols.partyvalues.mid.2008 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==8))
ols.partyvalues.mid.2012 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==9))
ols.partyvalues.mid.2016 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="mid" & time==10))
#
ols.partyonly.high.1988 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==3))
ols.partyonly.high.1992 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==4))
ols.partyonly.high.1996 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==5))
ols.partyonly.high.2000 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==6))
ols.partyonly.high.2004 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==7))
ols.partyonly.high.2008 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==8))
ols.partyonly.high.2012 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==9))
ols.partyonly.high.2016 <- lm(idealpoint ~ partyid, data=subset(values.df, sophistication=="high" & time==10))
#
ols.valuesonly.high.1988 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==3))
ols.valuesonly.high.1992 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==4))
ols.valuesonly.high.1996 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==5))
ols.valuesonly.high.2000 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==6))
ols.valuesonly.high.2004 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==7))
ols.valuesonly.high.2008 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==8))
ols.valuesonly.high.2012 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==9))
ols.valuesonly.high.2016 <- lm(idealpoint ~ egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==10))
#
ols.partyvalues.high.1988 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==3))
ols.partyvalues.high.1992 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==4))
ols.partyvalues.high.1996 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==5))
ols.partyvalues.high.2000 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==6))
ols.partyvalues.high.2004 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==7))
ols.partyvalues.high.2008 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==8))
ols.partyvalues.high.2012 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==9))
ols.partyvalues.high.2016 <- lm(idealpoint ~ partyid + egalitarianism + moraltraditionalism, data=subset(values.df, sophistication=="high" & time==10))
#
#
# OLS TABLES
#
apsrtable(ols.partyvalues.low.1988, ols.partyvalues.low.1992, ols.partyvalues.low.1996, ols.partyvalues.low.2000, ols.partyvalues.low.2004, ols.partyvalues.low.2008, ols.partyvalues.low.2012, ols.partyvalues.low.2016,
  model.names=paste(seq(1988,2016,by=4)), caption="Linear regression models of ideological ideal points: low political sophistication respondents")
#
apsrtable(ols.partyvalues.mid.1988, ols.partyvalues.mid.1992, ols.partyvalues.mid.1996, ols.partyvalues.mid.2000, ols.partyvalues.mid.2004, ols.partyvalues.mid.2008, ols.partyvalues.mid.2012, ols.partyvalues.mid.2016,
  model.names=paste(seq(1988,2016,by=4)), caption="Linear regression models of ideological ideal points: middle political sophistication respondents")
#
apsrtable(ols.partyvalues.high.1988, ols.partyvalues.high.1992, ols.partyvalues.high.1996, ols.partyvalues.high.2000, ols.partyvalues.high.2004, ols.partyvalues.high.2008, ols.partyvalues.high.2012, ols.partyvalues.high.2016,
  model.names=paste(seq(1988,2016,by=4)), caption="Linear regression models of ideological ideal points: high political sophistication respondents")
#
#
# OLS model with time trend
#
ols.idealpoint.low.time <- (lm(idealpoint ~ partyid + moraltraditionalism + egalitarianism + time + partyid:time + moraltraditionalism:time + egalitarianism:time, data=subset(values.df, sophistication=="low")))
ols.idealpoint.mid.time <- (lm(idealpoint ~ partyid + moraltraditionalism + egalitarianism + time + partyid:time + moraltraditionalism:time + egalitarianism:time, data=subset(values.df, sophistication=="mid")))
ols.idealpoint.high.time <- (lm(idealpoint ~ partyid + moraltraditionalism + egalitarianism + time + partyid:time + moraltraditionalism:time + egalitarianism:time, data=subset(values.df, sophistication=="high")))
#
apsrtable(ols.idealpoint.low.time, ols.idealpoint.mid.time, ols.idealpoint.high.time,
  model.names=c("Low sophistication", "Middle sophistication", "High sophistication"),
  caption="Determinants of ideological scores by level of political sophistication (American National Election Study, 1988-2016).")
#
#
#
#
stopCluster(cl)
#


library(tidyverse)
library(corrr)
library(igraph)
library(ggraph)

#
rawcorrelations <- data.frame(year=factor(dat[,"year"]), Y)[,1:19]
rawcorrelations$gayadoption <- rawcorrelations$gayadoption_2pt
rawcorrelations$gayadoption[rawcorrelations$year==1992] <- rawcorrelations$gayadoption_4pt[rawcorrelations$year==1992]
rawcorrelations$guncontrol <- rawcorrelations$guncontrol_3pt
rawcorrelations$guncontrol[rawcorrelations$year==1996] <- rawcorrelations$guncontrol_2pt[rawcorrelations$year==1996]
rawcorrelations$guncontrol[rawcorrelations$year==2000] <- rawcorrelations$guncontrol_5pt[rawcorrelations$year==2000]
rawcorrelations$guncontrol[rawcorrelations$year==2004] <- rawcorrelations$guncontrol_5pt[rawcorrelations$year==2004]
#
rawcorrelations <- rawcorrelations[,c(1:3,6,10:21)]
#
vars_keep <- names(rawcorrelations)[2:16]
#
some <- rawcorrelations %>% split(.$year) %>% map(select, vars_keep) %>% map(correlate, use="pair")
#
df.cor <- some %>% reshape2::melt() %>% rename(cor=value, year=L1)
df.cor$cor <- abs(df.cor$cor)
df.cor <- df.cor %>% filter(abs(cor) > .0)
df.cor <- df.cor %>% unite("z", term:variable)
#
df.cor <- arrange(df.cor, z, year)
df.cor <- df.cor[!duplicated(df.cor$cor),]
#
#
pdf("Dropbox/mass_constraint/images/ANES_rawcorrelations1.pdf", height=11.5, width=8.5)
#
ggplot(df.cor[1:210,], aes(x=as.numeric(year), y=cor, group=1)) +
	geom_point() +
	geom_smooth() +
	facet_wrap(~z, ncol=4) +
	xlab("\nYear") +
	scale_x_continuous(breaks=seq(1980,2016,12)) +
	scale_y_continuous(breaks=seq(0,0.8,0.4)) +
	ylab("r\n") +
	ggtitle("American National Election Studies: 1980-2016\n") +
	theme(plot.title=element_text(hjust = 0.5), axis.text.x = element_text(angle = 30, hjust = 1))
#
dev.off()
#
pdf("Dropbox/mass_constraint/images/ANES_rawcorrelations2.pdf", height=11.5, width=8.5)
#
ggplot(df.cor[211:420,], aes(x=as.numeric(year), y=cor, group=1)) +
	geom_point() +
	geom_smooth() +
	facet_wrap(~z, ncol=4) +
	xlab("\nYear") +
	scale_x_continuous(breaks=seq(1980,2016,12)) +
	scale_y_continuous(breaks=seq(0,0.8,0.4)) +
	ylab("r\n") +
	ggtitle("American National Election Studies: 1980-2016\n") +
	theme(plot.title=element_text(hjust = 0.5), axis.text.x = element_text(angle = 30, hjust = 1))
#
dev.off()
#
pdf("Dropbox/mass_constraint/images/ANES_rawcorrelations3.pdf", height=11.5, width=8.5)
#
ggplot(df.cor[421:617,], aes(x=as.numeric(year), y=cor, group=1)) +
	geom_point() +
	geom_smooth() +
	facet_wrap(~z, ncol=4) +
	xlab("\nYear") +
	scale_x_continuous(breaks=seq(1980,2016,12)) +
	scale_y_continuous(breaks=seq(0,0.8,0.4)) +
	ylab("r\n") +
	ggtitle("American National Election Studies: 1980-2016\n") +
	theme(plot.title=element_text(hjust = 0.5), axis.text.x = element_text(angle = 30, hjust = 1))
#
dev.off()
#
pdf("Dropbox/mass_constraint/images/ANES_rawcorrelations4.pdf", height=11.5, width=8.5)
#
ggplot(df.cor[618:719,], aes(x=as.numeric(year), y=cor, group=1)) +
	geom_point() +
	geom_smooth() +
	facet_wrap(~z, ncol=4) +
	xlab("\nYear") +
	scale_x_continuous(breaks=seq(1980,2016,12)) +
	scale_y_continuous(breaks=seq(0,0.8,0.4)) +
	ylab("r\n") +
	ggtitle("American National Election Studies: 1980-2016\n") +
	theme(plot.title=element_text(hjust = 0.5), axis.text.x = element_text(angle = 30, hjust = 1))
#
dev.off()
#
#
